How to create an internal XML DTD in Matlab?
1 view (last 30 days)
Show older comments
Hi everyone,
I'm trying to build a simple XML based database in Matlab.
For this purpose, each element needs a unique identifier. XML offers an attribute type "ID", which would perfectly suit this purpose - if I could get it to work.
From what I have read so far, I have to define the attribute in a Doctype Definition (DTD). I also found out that I can load an external DTD using the DOMImplementation.createDocumentType() function. However, this won't work for me because it will hardcode the path to the .dtd file into the resulting .xml file, which will cause issues with portability.
The most promising approach seems to be an internal DTD. There are many examples on what this would look like in XML (e.g. https://www.w3schools.com/xml/xml_dtd_intro.asp), but I can't find any clue on how to generate this in Matlab (or in Java, for that matter).
The resulting XML should look something like this:
<?xml version="1.0"?>
<!DOCTYPE sample [
<!ELEMENT sample ANY>
<!ATTLIST sample id ID #REQUIRED>
]>
What I've got so far is this:
obj.docNode = com.mathworks.xml.XMLUtils.createDocument('sample');
domImpl = obj.docNode.getImplementation();
doctype = domImpl.createDocumentType('sample', 'SYSTEM', 'sample.dtd');
obj.docNode.appendChild(doctype);
xmlwrite(obj.xmlpath, obj.docNode);
Which creates a reference to an external dtd file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sample
PUBLIC "SYSTEM" "sample.dtd">
<sample/>
... but that's nowhere near what I want.
It would be great if someone could point me in the right direction.
Thank you!
2 Comments
Guillaume
on 21 Aug 2019
Edited: Guillaume
on 22 Aug 2019
While your question is perfectly valid for this forum, I think you'll get more chance of finding help in a java forum. Matlab xml construction is entirely delegated to java. In my version, it uses apache xerxes' implementation which is (sort of!) documented at https://xerces.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/package-summary.html.
The documentation for the document class is at https://xerces.apache.org/xerces-j/apiDocs/org/apache/xerces/dom/CoreDocumentImpl.html
However, note that a DTD is not required for private xml (i.e. xml internal to your application). A DTD is useful if your xml is shared between applications that don't know the specificity of your XML, so that they can validate it.
edit: removed . that sneaked into a hyperlink
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!