2013-12-18 68 views
0

我是新手MATLAB用戶。我正在嘗試使用MATLAB中的值編寫XML方案。我在關閉標籤時遇到問題。該標籤在根之前關閉,但我想按要求關閉它。可能是一些語法錯誤 - 如果有人能糾正它。MATLAB:無法關閉標籤

%Generate XML // 
docNode = com.mathworks.xml.XMLUtils.createDocument('ESM_SIMULATION_TEST'); 
docRootNode = docNode.getDocumentElement; 

timestamp = docNode.createElement('TimeStamp'); 
timestamp.appendChild(docNode.createTextNode(time)); 
docRootNode.appendChild(timestamp); 

esmDevices = docNode.createElement('ESM_DEVICES'); 
docRootNode = docRootNode.appendChild(esmDevices); 

for i=1:tmp 
fileName = docNode.createElement(sprintf('ESM_ID_%d',i)); 
fileName.appendChild(docNode.createTextNode(files(i))); 
docRootNode.appendChild(fileName); 
end 

thisElement1 = docNode.createElement('Total_Devices'); 
thisElement1.appendChild(docNode.createTextNode(sprintf('%d',y))); 
esmDevices.appendChild(thisElement1); 

thisElement1 = docNode.createElement('TOTAL_AMPERAGE'); 
thisElement1.appendChild(docNode.createTextNode(sprintf('%d',int16(amp)))); 
esmDevices.appendChild(thisElement1); 

thisElement = docNode.createElement('RANDOM_ESM_DEVICES'); 
docRootNode = docRootNode.appendChild(thisElement); 

for i=1:rcountMW 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('Microwave')); 
    docRootNode.appendChild(thisElement); 
end 

for i=1:rcounttv 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('TV')); 
    docRootNode.appendChild(thisElement); 
end 

for i=1:rcountFan 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('Fan')); 
    docRootNode.appendChild(thisElement); 
end 

for i=1:rcountFL 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('Flourescent Lamp')); 
    docRootNode.appendChild(thisElement); 
end 

for i=1:rcountIL 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('Incadescent Lamp')); 
    docRootNode.appendChild(thisElement); 
end 

for i=1:rcountAC 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('AC')); 
    docRootNode.appendChild(thisElement); 
end 

for i=1:rcountDW 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('Dish Washer')); 
    docRootNode.appendChild(thisElement); 
end 

    for i=1:rcountWS 
    thisElement = docNode.createElement('Devices'); 
    thisElement.appendChild(docNode.createTextNode('Washer')); 
    docRootNode.appendChild(thisElement); 
    end 

    for i=1:rcountVC 
     thisElement = docNode.createElement('Devices'); 
     thisElement.appendChild(docNode.createTextNode('Vacumm Cleaner')); 
     docRootNode.appendChild(thisElement); 
    end 
thisElement = docNode.createElement('Randomizer_count_tv'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcounttv))); 
docRootNode.appendChild(thisElement); 

thisElement = docNode.createElement('Randomizer_count_Fan'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountFan))); 
docRootNode.appendChild(thisElement); 

thisElement = docNode.createElement('Randomizer_count_FL'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountFL))); 
docRootNode.appendChild(thisElement); 

thisElement = docNode.createElement('Randomizer_count_IncadescentLight'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountIL))); 
docRootNode.appendChild(thisElement); 

thisElement = docNode.createElement('Randomizer_count_dishwasher'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountDW))); 
docRootNode.appendChild(thisElement);  

thisElement = docNode.createElement('Randomizer_count_washer'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountWS))); 
docRootNode.appendChild(thisElement);   

thisElement = docNode.createElement('Randomizer_count_AC'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountAC))); 
docRootNode.appendChild(thisElement); 

thisElement = docNode.createElement('Randomizer_count_VC'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountVC))); 
docRootNode.appendChild(thisElement); 

thisElement = docNode.createElement('Randomizer_count_MW'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',rcountMW))); 
docRootNode.appendChild(thisElement);  

thisElement = docNode.createElement('Total_Random_Devices'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',a))); 
docRootNode.appendChild(thisElement); 

thisElement = docNode.createElement('Total_Randomizer_Amperage'); 
thisElement.appendChild(docNode.createTextNode(sprintf('%d',int16(rts)))); 
docRootNode.appendChild(thisElement); 

我想在這裏關閉ESM標記,但它在根之前關閉。下面是例子。 我想先關閉標籤。

<?xml version="1.0" encoding="utf-8"?> 
<ESM_SIMULATION_TEST> 
<TimeStamp>18-Dec-2013 17:19:14</TimeStamp> 
<ESM_DEVICES> 
    <ESM_ID_1>20136190344.xml</ESM_ID_1> 
    <ESM_ID_2>20136190445.xml</ESM_ID_2> 
    <Total_Devices>17</Total_Devices> 
    <TOTAL_AMPERAGE>69</TOTAL_AMPERAGE> 
    <RANDOM_ESM_DEVICES> 
    <Devices>TV</Devices> 
    <Devices>TV</Devices> 
    <Devices>TV</Devices> 
    <Devices>Fan</Devices> 
    <Devices>Flourescent Lamp</Devices> 
    <Devices>Flourescent Lamp</Devices> 
    <Devices>Incadescent Lamp</Devices> 
    <Devices>Incadescent Lamp</Devices> 
    <Devices>Incadescent Lamp</Devices> 
    <Devices>AC</Devices> 
    <Devices>AC</Devices> 
    <Randomizer_count_tv>3</Randomizer_count_tv> 
    <Randomizer_count_Fan>1</Randomizer_count_Fan> 
    <Randomizer_count_FL>2</Randomizer_count_FL> 
    <Randomizer_count_IncadescentLight>3</Randomizer_count_IncadescentLight> 
    <Randomizer_count_dishwasher>0</Randomizer_count_dishwasher> 
    <Randomizer_count_washer>0</Randomizer_count_washer> 
    <Randomizer_count_AC>2</Randomizer_count_AC> 
    <Randomizer_count_VC>0</Randomizer_count_VC> 
    <Randomizer_count_MW>0</Randomizer_count_MW> 
    <Total_Random_Devices>11</Total_Random_Devices> 
    <Total_Randomizer_Amperage>44</Total_Randomizer_Amperage> 
    </RANDOM_ESM_DEVICES> 

+0

「標籤在root_之前關閉_at但是我想關閉它_at required_。」。咦?你能展示你正在生成的XML,以及你想得到什麼? – Floris

+0

@弗洛里斯:我編輯了這個問題,你可以在那裏看到XML。 請讓我知道如何在 MAV

+0

之前關閉標籤您是否可以包含重現您所看到的XML所需的所有代碼 - 現在有一些未初始化的變量。另外,顯示兩個XML:**代碼創建它**,AND **,因爲您希望它看起來**,而不是說「我要關閉ESM標記,但它在根之前關閉」。 – Floris

回答

1

我認爲這個問題是在你行

docRootNode = docRootNode.appendChild(thisElement); 

您正在重新定義文件的docRootNode - 之後,一切都處於關閉狀態。將其更改爲

docRootNode.appendChild(thisElement); 

認爲一切都會好起來。不幸的是,我現在無法測試這個 - 不靠近使用Matlab的計算機。