[英]Writing an XML in Matlab: How to add reference to DTD?
I'm trying to write an XML file using Matlab and I need to specify a DOCTYPE DTD at the header, but I haven't found any method for this in the Matlab documentation or questions related. 我正在尝试使用Matlab编写XML文件,我需要在标题中指定DOCTYPE DTD,但是我没有在Matlab文档或相关问题中找到任何方法。 Every question involving a DTD reference is about how to read an XML into Matlab.
涉及DTD参考的每个问题都是关于如何将XML 读入Matlab。
What I am able to do now is an XML file of the type 我现在能做的是这种类型的XML文件
<?xml version="1.0"?>
<root>
<child>
Hello world!
</child>
</root>
with the code 与代码
docNode = com.mathworks.xml.XMLUtils.createDocument('root');
root = docNode.getDocumentElement;
child = docNode.createElement('child');
child.appendChild(docNode.createTextNode('Hello World!'));
root.appendChild(child);
xmlwrite(docNode)
However, I need the file to include a DTD reference: 但是,我需要该文件包含DTD引用:
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "root.dtd" []>
<root>
<child>
Hello world!
</child>
</root>
Is there any function in com.mathworks.xml.XMLUtils for this? com.mathworks.xml.XMLUtils中有没有这个功能? Or will I have to open the generated XML and manually insert the DTD reference?
或者我是否必须打开生成的XML并手动插入DTD引用?
You can stay with using the org.w3c.dom package: you can use the createDocumentType method of DOMImplementation . 您可以继续使用org.w3c.dom包:您可以使用DOMImplementation的createDocumentType方法。
domImpl = docNode.getImplementation();
doctype = domImpl.createDocumentType('root', 'SYSTEM', 'root.dtd');
With this update the full sample code is: 通过此更新,完整的示例代码为:
docNode = com.mathworks.xml.XMLUtils.createDocument('root');
domImpl = docNode.getImplementation();
doctype = domImpl.createDocumentType('root', 'SYSTEM', 'root.dtd');
docNode.appendChild(doctype);
root = docNode.getDocumentElement;
child = docNode.createElement('child');
child.appendChild(docNode.createTextNode('Hello World!'));
root.appendChild(child);
xmlwrite(docNode)
Output 产量
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root PUBLIC "SYSTEM" "root.dtd">
<root>
<child>Hello World!</child>
</root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.