Create XML with document name and attributtes
Show older comments
Hi,
I am developping a code to create an XML document from data retrived from an excel sheet. The requirement is such that, the XML document node should be off the following format:
<sce xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.5">
<Scenario>
<description>...</description>
<name/>
</Scenario>
</sce>
The XML that i am creating comes out as below:
<sce>
<Scenario>
<description>...</description>
<name/>
</Scenario>
</sce>
i.e i am not able to create the attributes of the document node which are xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.5"
I have the attrbutes and the corresponding values in a container map as seen below:
objScenarioDocDetails.cm_docAttributes= containers.Map(arrAttributesKeySet,arrAttributesValueSet);
Any help to fix this issue would be greatly appreaciated. Thanks in advance.
Answers (1)
zhuofei wu
on 16 Sep 2022
0 votes
try this:
import matlab.io.xml.dom.*
docNode = Document("sce");
docRootNode = getDocumentElement(docNode);
docRootNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
docRootNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "version", "1.5");
xmlFileName = "example_ns.xml";
writer = matlab.io.xml.dom.DOMWriter;
writer.Configuration.FormatPrettyPrint = true;
writeToFile(writer,docNode,xmlFileName);
type(xmlFileName);
The explaination could be found at: https://stackoverflow.com/a/11146971/9173213
Categories
Find more on Structured Data and XML Documents in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!