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?