Reputation: 92
I am novice MATLAB user. I am trying to write a XML scheme using the values from the MATLAB. I am facing problem in closing the tag. The tag closes before the root but I want to close it at the required. Might be some syntax error- If anyone can correct it.
%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);
I want to close the ESM tag here but it closes before the root. Below is the example. I want the close the tag before .
<?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>
Upvotes: 0
Views: 96
Reputation: 92
There is a very good example in the MATLAB help for writing a XML. Just try to run in once and play around it with and you should be able to understand to how to append and close the tags at the desired. http://www.mathworks.com/help/matlab/ref/xmlwrite.html
Upvotes: 1
Reputation: 46395
I think the problem is in your line
docRootNode = docRootNode.appendChild(thisElement);
You are redefining the docRootNode
of the document - after that everything is off. Change it to
docRootNode.appendChild(thisElement);
and I think all will be well. Unfortunately I can't test this right now - not near a computer with Matlab.
Upvotes: 1