MATLAB:无法关闭标签

时间:2013-12-18 23:00:47

标签: xml matlab

我是新手MATLAB用户。我正在尝试使用MATLAB中的值编写XML方案。我在关闭标签时遇到问题。标签在根之前关闭但我想在需要时关闭它。可能是一些语法错误 - 如果有人可以纠正它。

%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>

2 个答案:

答案 0 :(得分:1)

我认为问题出在你的行中

docRootNode = docRootNode.appendChild(thisElement); 

您正在重新定义文档的docRootNode - 之后一切都已关闭。将其更改为

docRootNode.appendChild(thisElement); 

而我认为一切都会好起来的。不幸的是我现在无法测试它 - 不是靠近有Matlab的计算机。

答案 1 :(得分:1)

MATLAB帮助编写XML有一个非常好的例子。 试着跑一次然后玩它,你应该这样做 能够理解如何追加和关闭标签 期望。 http://www.mathworks.com/help/matlab/ref/xmlwrite.html