How to create an internal XML DTD in Matlab?

1 回表示 (過去 30 日間)
Tobias Kowalski
Tobias Kowalski 2019 年 8 月 21 日
編集済み: Guillaume 2019 年 8 月 22 日
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 件のコメント
Guillaume
Guillaume 2019 年 8 月 21 日
編集済み: Guillaume 2019 年 8 月 22 日
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.
You may find more information at the project homepage.
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
Tobias Kowalski
Tobias Kowalski 2019 年 8 月 22 日
Thank you for your comment, Guillaume. Asking in a java forum might be my next step, but I'm still hesitant because I have only very basic java and XML knowledge and it's already getting quite confusing.
Also, thank you for pointing out the xerces documentation. Looks as if the createElementDefinition() function might be worth a try, even tough I still can't see how to add an <!ATTLIST>. But I'm definitely going to try this out later.
As for wether an DTD is required: If everything works as planned, the resulting XML files might very well be shared across different applications, which might even be based on different programming languages, running on different operating systems. This is why I'm using XML in the first place, why I'm trying to use common DOM features such as getElementById() instead of hardcoding attribute types in my application, and also why an external DTD file probably wouldn't work.
In the meantime, I would welcome any other recommondations. Maybe someone knows of another way of getting getElementById() to work?

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeStructured Data and XML Documents についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by