XML Help - How to Change encoding? How to set DOCTYPE? Why cant I find any documentatio?

조회 수: 32 (최근 30일)
Hello, im trying to create an xml and i need to follow a very specific format. This included encoding with ISO-8859-1 and having document type of <!DOCTYPE CAMERA SYSTEM "camera_cal.dtd">
The very few matlab xml help i found online mention using functions like:
com.mathworks.xml.XMLUtils.createDocument();
getImplementation();
createDocumentType();
etc.
Why cant i find any documentation on inputs to these functions or other xml functions anywhere on mathworks? People know these functions exists, where did they learn of them?
Currently the closest match to the doc type ive gotten is this result: <!DOCTYPE CAMERA SYSTEM "camera_cal.dtd" PUBLIC "SYSTEM">, which isnt exactly what i wanted, specifically the last "PUBLIC 'SYSTEM'" bit.
Also i cant find anything about changing the encoding from UTF-8 to ISO-8859
Any help would be greatly appreciated. Thanks

채택된 답변

Kojiro Saito
Kojiro Saito 2020년 12월 4일
I think there's no way to customize encoding in xmlwrite and writestruct (from R2020b).
Workaround would be using .NET or Java libraries.
If you're using Windows machine, you can use .NET addAssembly and you can utilize System.XML libraries in MATLAB.
asm = NET.addAssembly('System.XML');
import System.XML.*
% Create XML object
doc = System.Xml.XmlDocument;
% Specify encoding ISO-8859-1
xmldecl = doc.CreateXmlDeclaration('1.0', 'ISO-8859-1', []);
doc.AppendChild(xmldecl);
% Specify DocumentType
doc.XmlResolver= []; % To avoid dtd search errors.
doctype = doc.CreateDocumentType("CAMERA", [], "camera_cal.dtd", []);
doc.AppendChild(doctype);
% Create an element. This is just a sample.
newElem = doc.CreateElement("items");
newNode = doc.CreateNode("element", "item", []);
attr = doc.CreateAttribute("name");
attr.Value = "name1";
newNode.SetAttributeNode(attr);
newNode.InnerText = "text here";
newElem.AppendChild(newNode);
doc.AppendChild(newElem);
% Save the XML object to xml file
settings = System.Xml.XmlWriterSettings();
settings.Indent = true;
writer = System.Xml.XmlWriter.Create("out.xml", settings);
doc.WriteTo(writer);
writer.Close;
Then, you can get the following xml file.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE CAMERA SYSTEM "camera_cal.dtd">
<items>
<item name="name1">text here</item>
</items>
  댓글 수: 2
Sergio Huerta
Sergio Huerta 2020년 12월 4일
Thanks a lot. appreciate it!. Also do you know why there dont seem to be any docs on matlab's XMLUtils functions/methods? Or if they do exist, where are they located?
Kojiro Saito
Kojiro Saito 2020년 12월 7일
Sure. com.mathworks.xml.XMLUtils is Java method and doesn't seem to have detail documentation. It is Java wrapper of Java API for XML (org.w3c.dom) and there is document of org.w3c.dom.
As described in this page, MATLAB R2020b supports .NET 4.0 and above, so I've provided .NET approach based on this System.Xml document.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by