XML write set attribute with xml namespace

10 ビュー (過去 30 日間)
vivek patel
vivek patel 2023 年 3 月 9 日
編集済み: Meet 2024 年 9 月 11 日
I am trying to write an xml file, where i need to write the xml namespace and its URI as below.
<node xmlns:xml="http://www.w3.org/XML/1998/namespace" >
......
.....
</node>
I understand that this is completely optional and its okay if we don't write it. But i need it as that is expected to be done.
I am using xmlwrite function as using older matlab version, so don't have writeStruct function. When i am writing below code somehow matlab is not writing the xml namespace in the xml file. I think it is because it is preserved but i am not able to get any workaround it.
Can you please help on this ?
docNode = com.mathworks.xml.XMLUtils.createDocument('node');
rootElement = docNode.getDocumentElement;
rootElement.setAttribute('xmlns:xml','http://www.w3.org/XML/1998/namespace');
rootElement.setAttribute('xmlns:2xml','http://www.w3.org/XML/1998/namespace');
xmlwrite('test.xml',docNode);
  2 件のコメント
Mihai
Mihai 2023 年 3 月 11 日
Try:
docNode = com.mathworks.xml.XMLUtils.createDocument('node');
rootElement = docNode.getDocumentElement;
rootElement.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xml', 'http://www.w3.org/XML/1998/namespace');
vivek patel
vivek patel 2023 年 3 月 16 日
Hi,
I tried but unfortunately it is not working, i have tried in MATLAB 2016b and MATLAB 2021a versions. Is it working in some other versions ?

サインインしてコメントする。

回答 (1 件)

Meet
Meet 2024 年 9 月 11 日
編集済み: Meet 2024 年 9 月 11 日
Hi Vivek,
The “xml” prefix is reserved, which may cause MATLAB to omit it during the writing process.
As a workaround, you might consider manually editing the XML file after it is generated to include the namespace, you can look it into the code below:
% Import all classes and functions from the matlab.io.xml.dom package
import matlab.io.xml.dom.*
% Create a new XML document with a root element named "node"
docNode = Document("node");
% Retrieve the root element of the XML document
docRootNode = getDocumentElement(docNode);
% Set an attribute with a namespace for the root element
% "http://www.w3.org/2000/xmlns/" is the standard URI for XML namespaces
% "xmlns:xml" is the attribute name, and "http://www.w3.org/XML/1998/namespace" is its value
docRootNode.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xml", "http://www.w3.org/XML/1998/namespace");
% Name of the XML file
xmlFileName = "test.xml";
% Create a DOMWriter object to handle writing the XML document to a file
writer = matlab.io.xml.dom.DOMWriter;
% Write the XML document to the specified file using the writer object
writeToFile(writer, docNode, xmlFileName);
Note: matlab.io.xml.dom.DOMWriter was introduced in MATLAB R2021a.
You can refer to the resources below for more information:
  1. DOMWriter: https://www.mathworks.com/help/matlab/ref/matlab.io.xml.dom.domwriter-class.html
  2. setAttributeNS: https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttributeNS

カテゴリ

Help Center および File ExchangeStructured 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