Create XML with document name and attributtes

1 visualizzazione (ultimi 30 giorni)
Giri
Giri il 21 Mar 2018
Risposto: zhuofei wu il 16 Set 2022
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.

Risposte (1)

zhuofei wu
zhuofei wu il 16 Set 2022
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

Community Treasure Hunt

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

Start Hunting!

Translated by