Create a nested XML file

2 views (last 30 days)
Duke
Duke on 11 Oct 2013
Commented: Duke on 16 Oct 2013
I would like to create an XML file where elements can be nested inside each other. I haven't found much documentation of this and I have got this working without nesting but when I try to next elements inside of each other I don't get my expected output. Take the following example (taken from Mathworks website):
docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
docRootNode = docNode.getDocumentElement;
docRootNode.setAttribute('attr_name','attr_value');
for i=1:5
thisElement = docNode.createElement('child_node');
thisElement.appendChild(docNode.createTextNode(sprintf('%i',i)));
docRootNode.appendChild(thisElement);
end
docNode.appendChild(docNode.createComment('this is a comment'));
xmlFileName = ['tempname.xml'];
xmlwrite(xmlFileName,docNode);
type(xmlFileName);
This give the output (as I would expect):
<?xml version="1.0" encoding="utf-8"?>
<root_element attr_name="attr_value">
1
2
3
4
5
</root_element><!--this is a comment-->
But when I try to add another node into the picture:
docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
docRootNode = docNode.getDocumentElement;
docRootNode.setAttribute('attr_name','attr_value');
for i=1:5
thisElement = docNode.createElement('child_node');
thisElement.appendChild(docNode.createTextNode(sprintf('%i',i)));
docRootNode.appendChild(thisElement);
nestedElement = docNode.createElement('nested_node');
nestedElement.appendChild(docNode.createTextNode(sprintf('%i',i + 10)));
thisElement.appendChild(nestedElement);
end
docNode.appendChild(docNode.createComment('this is a comment'));
xmlFileName = ['tempname.xml'];
xmlwrite(xmlFileName,docNode);
type(xmlFileName);
I get this output:
<?xml version="1.0" encoding="utf-8"?>
<root_element attr_name="attr_value">
1<nested_node>11</nested_node>
2<nested_node>12</nested_node>
3<nested_node>13</nested_node>
4<nested_node>14</nested_node>
5<nested_node>15</nested_node>
</root_element><!--this is a comment-->
But this is the input I was expecting:
<?xml version="1.0" encoding="utf-8"?>
<root_element attr_name="attr_value">
1
<nested_node>11</nested_node>
2
<nested_node>12</nested_node>
3
<nested_node>13</nested_node>
4
<nested_node>14</nested_node>
5
<nested_node>15</nested_node>
</root_element><!--this is a comment-->
Is there any way that you can add in the newlines from the code?
  1 Comment
Duke
Duke on 16 Oct 2013
I found out something interesting, if I comment out one line I get the results that I would expect:
docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
docRootNode = docNode.getDocumentElement;
docRootNode.setAttribute('attr_name','attr_value');
for i=1:5
thisElement = docNode.createElement('child_node');
%thisElement.appendChild(docNode.createTextNode(sprintf('%i',i)));
docRootNode.appendChild(thisElement);
nestedElement = docNode.createElement('nested_node');
nestedElement.appendChild(docNode.createTextNode(sprintf('%i',i + 10)));
thisElement.appendChild(nestedElement);
end
docNode.appendChild(docNode.createComment('this is a comment'));
xmlFileName = ['tempname.xml'];
xmlwrite(xmlFileName,docNode);
type(xmlFileName);
Which produces:
<?xml version="1.0" encoding="utf-8"?>
<root_element attr_name="attr_value">
<nested_node>11</nested_node>
<nested_node>12</nested_node>
<nested_node>13</nested_node>
<nested_node>14</nested_node>
<nested_node>15</nested_node>
</root_element><!--this is a comment-->
So if I remove the wording I get the layout that I would expect. Is it possible to “have my cake and eat it too” by getting this layout with the wording or is that improper XML format and that’s why I’m getting the un-nested result when I have the wording in the child_node?

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!