Xml MATLAB:无法关闭标记

Xml MATLAB:无法关闭标记,xml,matlab,Xml,Matlab,我是MATLAB的新手用户。我正在尝试使用MATLAB中的值编写一个XML方案。我在关闭标签时遇到问题。标记在根之前关闭,但我希望在所需的位置关闭它。可能是一些语法错误-如果有人可以纠正的话 %Generate XML // docNode = com.mathworks.xml.XMLUtils.createDocument('ESM_SIMULATION_TEST'); docRootNode = docNode.getDocumentElement; timestamp = docNo

我是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>

2013年12月18日17:19:14
20136190344.xml
20136190445.xml
十七,
69
电视
电视
电视
扇子
荧光灯
荧光灯
白炽灯
白炽灯
白炽灯
自动控制
自动控制
3.
1.
2.
3.
0
0
2.
0
0
11
44

我想问题出在你这一行

docRootNode = docRootNode.appendChild(thisElement); 
您正在重新定义文档的
docRootNode
,之后一切都将关闭。换成

docRootNode.appendChild(thisElement); 

我想一切都会好起来的。不幸的是,我现在不能测试这一点——不能靠近使用Matlab的计算机。

在Matlab帮助中有一个编写XML的非常好的例子。 只要试着跑进去玩一次,你就应该 能够理解如何在 渴望的

“标记在根之前关闭,但我希望在所需的位置关闭它。”。嗯?你能展示一下你正在生成的XML,以及你想要得到什么吗?@Floris:我已经编辑了这个问题,你可以在那里看到XML。请让我知道如何在测试之前关闭标记,您是否可以包含复制您看到的XML所需的所有代码-现在有一些未初始化的变量。另外,显示两组XML:代码创建时,以及您希望看到的XML,而不是说“我想在这里关闭ESM标记,但它在根之前关闭”。@Floris:我在下面添加了一个示例,并将整个代码放在下面。我想在之前关闭标记。@Floris:不走运:(。我无法关闭它。我想整个语法都有问题。你真的能解决它吗?或者我得到一个示例问题,以便在完成后可以解决。很高兴你找到了该引用!