Create XML with document name and attributtes

4 次查看(过去 30 天)
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.

回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Structured Data and XML Documents 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by