2017-07-13 3 views
0

現在、大きなXMLファイルがあり、ヘルプを使って新しいxmlファイルに変更してエクスポートしたいR. のここで(それが最後まで同じに静かまま)私のxmlファイルの先頭にあるxml-fileから変数のループを組み合わせて新しいxml-fileに保存する方法

<Assemblies count="4"> 
<Assembly index="0"> 
    <IdentNr>2</IdentNr> 
    <IDNr_DB>0</IDNr_DB> 
    <Name>Decke D1</Name> 
    <Order_Layers choice="von außen nach innen">2</Order_Layers> 
    <Grid_Kind choice="Mittel">2</Grid_Kind> 
    <Layers count="5"> 
    <Layer index="0"> 
     <Thickness unit="m">0.003</Thickness> 
     <Material> 
     <IDNr_DB>1203</IDNr_DB> 
     <Name>Linoleum nach DIN 18171</Name> 
     <ThermalConductivity unit="W/mK">0.17</ThermalConductivity> 
     <BulkDensity unit="kg/m³">1000</BulkDensity> 
     <Porosity unit="-">0.23</Porosity> 
     <HeatCapacity unit="J/kgK">1500</HeatCapacity> 
     <WaterVaporResistance unit="-">6250</WaterVaporResistance> 
     <ReferenceWaterContent unit="kg/m³" /> 
     <FreeWaterSaturation unit="kg/m³" /> 
     <WaterAbsorptionCoefficient unit="kg/m²s^0.5" /> 
     <MoistureSupplement unit="%/M.-%" /> 
     <TempDepThermalCondSupplement unit="W/mK²" /> 
     <TypicalMoisture unit="kg/m³" /> 
    <Layer index="1"> 
     <Thickness unit="m">0.02</Thickness> 
     <Material> 
     <IDNr_DB>1039</IDNr_DB> 
     <Name>Zement Fließestrich, obere Schicht</Name> 
     <ThermalConductivity unit="W/mK">1.6</ThermalConductivity> 
     <BulkDensity unit="kg/m³">1890</BulkDensity> 
     <Porosity unit="-">0.2</Porosity> 
     <HeatCapacity unit="J/kgK">850</HeatCapacity> 
     <WaterVaporResistance unit="-">58</WaterVaporResistance> 
     <ReferenceWaterContent unit="kg/m³">37.8</ReferenceWaterContent> 
     <FreeWaterSaturation unit="kg/m³">168</FreeWaterSaturation> 
     <WaterAbsorptionCoefficient unit="kg/m²s^0.5">0.025</WaterAbsorptionCoefficient> 
     <MoistureSupplement unit="%/M.-%" /> 
     <TempDepThermalCondSupplement unit="W/mK²">0.0002</TempDepThermalCondSupplement> 
     <TypicalMoisture unit="kg/m³">168</TypicalMoi 

私はすでにforeachループを使用してコードを持っているが、私は、変数のトンを変更する必要があります。ここでは6つの新しいファイル

library(XML) 
doc <- xmlTreeParse("d:\\Users\\Documents\\raum-klima-putz\\R\\TestXML\\test.xml", getDTD = F) 
r <- xmlRoot(doc) 
ExpPath <- "d:\\Users\\Documents\\raum-klima-putz\\R\\TestXML3" 
example.weatherfile <- c("d:\\Users\\Documents\\raum-klima-putz\\R\\WetterdatenJuni2017\\2032_Karlsruhe.epw", "d:\\Users\\Documents\\raum-klima-putz\\R\\WetterdatenJuni2017\\2032_Karlsruhe_swdirnorm.epw", "Test") 
example.thickness <- c("12","20","21") 
for (i in 1:length(example.weatherfile)) 
    { 
xmlValue(r[["Variants"]][[1]][["ClimateLocation"]][["FileName"]]) <- example.weatherfile[i] 
xmlValue(r[["Assemblies"]][[1]][["Layers"]][[1]][["Thickness"]]) <- example.thickness[i] 
FileName  <- paste("Weather_neu",i, ".xml", sep=""); 
saveXML(r, file=paste(ExpPath, FileName, sep = "\\"), compression=0, prefix = NULL); 
} 
for (i in 1:length(example.thickness)) 
    { 
xmlValue(r[["Variants"]][[1]][["ClimateLocation"]][["FileName"]]) <- example.weatherfile[i] 
xmlValue(r[["Assemblies"]][[1]][["Layers"]][[1]][["Thickness"]]) <- example.thickness[i] 
FileName  <- paste("Thickness_neu",i, ".xml", sep=""); 
saveXML(r, file=paste(ExpPath, FileName, sep = "\\"), compression=0, prefix = NULL); 
} 

ではなく、私は私がすべてのパラメータのcombinbinationを得ることができるので、行列のようなものを作成したい変数の組み合わせごとにforsearchループを作成するのをエクスポートする2つのループを持つ例があり、例えば10個のパラメータの場合:100個の新しいファイルを取得する10x10の行列。したがって、最初のパラメータは3つの変数に置き換えられ、他のすべてのパラメータ、次に2番目のパラメータなどと組み合わされます。 これらのバリエーションを作成するより簡単な方法は、すべてのパラメータに対してforeachループを作成するよりも簡単ですか? ありがとうございます

+0

XMLが不完全であるようです。 –

答えて

0

これはあなたの後にあなたを得るために働くかもしれません。

library(XML) 

doc <- xmlTreeParse("d:\\Users\\Documents\\raum-klima-putz\\R\\TestXML\\test.xml", getDTD = F) 
r <- xmlRoot(doc) 

ExpPath <- "d:\\Users\\Documents\\raum-klima-putz\\R\\TestXML3" 

example.weatherfile <- c("d:\\Users\\Documents\\raum-klima-putz\\R\\WetterdatenJuni2017\\2032_Karlsruhe.epw", "d:\\Users\\Documents\\raum-klima-putz\\R\\WetterdatenJuni2017\\2032_Karlsruhe_swdirnorm.epw", "Test") 
example.thickness <- c("12","20","21") 

# Create dataframe with all combinations of weatherfiles and thicknesses 
all.combos <- expand.grid(example.weatherfile, example.thickness) 
# Add a column for row number 
all.combos$rownum <- c(1:nrow(all.combos)) 

# Apply the custom function to all rows of the all.combos dataframe 
apply(all.combos, 1, function(x){ 
         # x is a vector of the current row being processed 
         # x[1] is the current weatherfile 
         # x[2] is the current thickness 
         # x[3] is the current row number 
         xmlValue(r[["Variants"]][[1]][["ClimateLocation"]][["FileName"]]) <- x[1] 
         xmlValue(r[["Assemblies"]][[1]][["Layers"]][[1]][["Thickness"]]) <- x[2] 
         FileName <- paste("Weather_neu",x[3], ".xml", sep=""); 
         saveXML(r, file=paste(ExpPath, FileName, sep = "\\"), compression=0, prefix = NULL)}) 
関連する問題