{"id":382,"date":"2010-06-28T15:37:26","date_gmt":"2010-06-28T15:37:26","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2010\/06\/28\/using-xml-in-matlab\/"},"modified":"2010-06-28T15:37:26","modified_gmt":"2010-06-28T15:37:26","slug":"using-xml-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2010\/06\/28\/using-xml-in-matlab\/","title":{"rendered":"Using XML in MATLAB"},"content":{"rendered":"<p>Much of the data on the Internet is stored in some flavor of XML. Fortunately for us, MATLAB has some built in functions for handling XML file. This will be the first in a series of non-consecutive posts about working with XML in MATLAB. Today I'm going to describe the functions for reading, writing, and transforming XML files. <\/p>\n<p>There are three functions in MATLAB to specifically deal with XML files. The first is <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/xmlread.html\"><tt>xmlread<\/tt><\/a>. This function takes either a URL or a filename and creates a Java XML object in the workspace:<\/p>\n<p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xmlfile = fullfile(matlabroot, <span style=\"color: #A020F0\">'toolbox\/matlab\/general\/info.xml'<\/span>);\r\nxDoc = xmlread(xmlfile)<\/pre>\n<pre style=\"font-style:oblique\"> \r\nxDoc =\r\n \r\n[#document: null]\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Don't worry that the return value says: \"<tt>[#document: null]<\/tt>\". The <tt>xmlread<\/tt> function returns a Java object that represents the file's <a href=\"http:\/\/www.w3.org\/DOM\/\">Document Object Model<\/a>, or DOM. The \"<tt>null<\/tt>\" is simply what the <tt>org.apache.xerces.dom.DeferredDocumentImpl<\/tt>'s implementation of <tt>toString()<\/tt> dumps to the MATLAB Command Window. To learn more about interacting with Java objects in MATLAB, see <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/07\/06\/calling-java-from-matlab\/\">my previous article<\/a>. <\/p>\n<p>Just to make sure this object has all our XML text, let's use the next MATLAB XML function: <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/xmlwrite.html\"><tt>xmlwrite<\/tt><\/a>. Without any additional arguments, <tt>xmlwrite<\/tt> will display the contents of the DOM in the Command Window:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xmlwrite(xDoc)<\/pre>\n<pre style=\"font-style:oblique\">\r\nans =\r\n\r\n&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;productinfo xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"https:\/\/www.mathworks.com\/namespace\/info\/v1\/info.xsd\"&gt;\r\n&lt;?xml-stylesheet type=\"text\/xsl\" href=\"https:\/\/www.mathworks.com\/namespace\/info\/v1\/info.xsl\"?&gt;\r\n\r\n   &lt;matlabrelease&gt;14&lt;\/matlabrelease&gt;\r\n   &lt;name&gt;MATLAB&lt;\/name&gt;\r\n   &lt;type&gt;matlab&lt;\/type&gt;\r\n   &lt;icon&gt;ApplicationIcon.MATLAB&lt;\/icon&gt;\r\n   &lt;help_location&gt;$docroot\/techdoc&lt;\/help_location&gt;\r\n   &lt;dialogpref_registrar&gt;   \r\n      &lt;source&gt;com.mathworks.mde.editor.EditorOptions&lt;\/source&gt;\r\n   &lt;\/dialogpref_registrar&gt;\r\n      &lt;!-- cut for brevity..... --&gt;\r\n   &lt;\/list&gt;\r\n&lt;\/productinfo&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Of course, you can also use <tt>xmlwrite<\/tt> to save the XML document to disk, by calling <tt>xmlwrite<\/tt> with the following signature:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xmlwrite(outfile,xDoc)<\/pre>\n<p>One gotcha when working with <tt>xmlread<\/tt> is that because the created DOM object is a Java object, it is stored in Java memory. The amount of memory set aside for Java is system dependent, but is generally between 64 and 256 MB. This means that if you read in a 200MB XML file, you're going to run out of memory, no matter how much free main memory MATLAB says is available. In that case, you just need to adjust the amount of memory available to Java, using <a href=\"https:\/\/blogs.mathworks.com\/community\/2010\/04\/26\/controlling-the-java-heap-size\/\">the new preference panel<\/a>. <\/p>\n<p>The final XML function provided by MATLAB deals with transforming XML documents with <a href=\"http:\/\/www.w3.org\/TR\/xslt\">XSL Stylesheets<\/a>. The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/xslt.html\"><tt>xslt<\/tt><\/a> function takes a DOM object or a filename or url specifying an XML document, a filename or url specifying an XSL stylesheet, and the output destination and performs the transform. Working with stylesheets is pretty complicated so I'm not going to delve into them here.<\/p>\n<p>Next time, I'll talk about the DOM object itself and how to add, remove, and query nodes. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Much of the data on the Internet is stored in some flavor of XML. Fortunately for us, MATLAB has some built in functions for handling XML file. This will be the first in a series of non-consecutive... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2010\/06\/28\/using-xml-in-matlab\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/382"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=382"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}