MATLAB: XML Help – How to Change encoding? How to set DOCTYPE? Why cant I find any documentatio

document typeencodingMATLABmatlab 2020xml

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

Best Answer

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>