{"id":655,"date":"2012-02-20T14:00:22","date_gmt":"2012-02-20T19:00:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/?p=655"},"modified":"2012-02-21T08:37:49","modified_gmt":"2012-02-21T13:37:49","slug":"even-more-xml-and-matlab-controlling-xml-output","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2012\/02\/20\/even-more-xml-and-matlab-controlling-xml-output\/","title":{"rendered":"Even More XML and MATLAB : Controlling XML Output"},"content":{"rendered":"<p>I&#8217;ve had a lot of comments in the past few months about MATLAB&#8217;s XML functionality, so I guess it&#8217;s time to provide a few more posts on the matter. Today is part 4 of the series, this time covering some of the limitations of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/xmlwrite.html\" target=\"_blank\"><tt>xmlwrite<\/tt><\/a>.<\/p>\n<p>XMLWRITE is MATLAB&#8217;s function for serializing the document element. With just one argument, the xml content is printed to the Command Window. With two arguments that data is saved to a text file. This function doesn&#8217;t give you a lot of room to customize the output; we pretty much expect that most MATLAB users will want the default. However, there are times when you&#8217;ll need a little extra control over the output. The advanced concept here is that although you create an XML document object in the MATLAB workspace, the output is really controlled by a Java serializer object, and does not use all of the document&#8217;s properties. XMLWRITE creates and executes the serializer for you. To customize this process you&#8217;ll have to manage the serialization yourself using MATLAB&#8217;s Java interface.<\/p>\n<p>In particular, the properties set on the XML node in the output file are set by the serializer and not the DOM object passed to XMLWRITE. This limitation was encountered by some of my blog&#8217;s commenters and users of MATLAB Answers. For an example, see this MATLAB Answers post: &#8220;<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/4981-setxmlstandalone-bug\" target=\"_blank\">setXmlStandalone bug?<\/a>.&#8221; The workaround I presented in that post, I&#8217;ve duplicated below. Fair warning, this is an advanced Java maneuver. <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">docNode = com.mathworks.xml.XMLUtils.createDocument(<span style=\"color: #A020F0\">'AddressBook'<\/span>);\r\ndocNode.getDocumentElement.appendChild(docNode.createElement(<span style=\"color: #A020F0\">'b'<\/span>));\r\ndocNode.getDocumentElement.appendChild(docNode.createElement(<span style=\"color: #A020F0\">'a'<\/span>));\r\n<span style=\"color: #228B22\">%don't do this docNode.setXmlStandalone(1)<\/span>\r\n\r\nimport <span style=\"color: #A020F0\">javax.xml.transform.*<\/span>;\r\nimport <span style=\"color: #A020F0\">javax.xml.transform.dom.*<\/span>;\r\nimport <span style=\"color: #A020F0\">javax.xml.transform.stream.*<\/span>;  \r\n\r\ntfactory = <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/xml\/transform\/TransformerFactory.html\" target=\"_blank\">TransformerFactory<\/a>.newInstance;\r\n<a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/xml\/transform\/Transformer.html\" target=\"_blank\">transformer <\/a>= tfactory.newTransformer;\r\nsrc = <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/xml\/transform\/dom\/DOMSource.html\">DOMSource<\/a>(docNode);\r\nstream = <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/java\/io\/StringWriter.html\">java.io.StringWriter<\/a>;\r\ndst = <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/xml\/transform\/stream\/StreamResult.html\">StreamResult<\/a>(stream);\r\n\r\n<span style=\"color: #228B22\">%set the value here instead<\/span>\r\ntransformer.setOutputProperty(OutputKeys.STANDALONE,<span style=\"color: #A020F0\">'yes'<\/span>);\r\ntransformer.setOutputProperty(OutputKeys.VERSION,<span style=\"color: #A020F0\">'1.1'<\/span>);\r\ntransformer.setOutputProperty(OutputKeys.ENCODING,<span style=\"color: #A020F0\">'ASCII'<\/span>);\r\n\r\ntransformer.transform(src,dst);\r\n\r\nresult = char(stream.toString)<\/pre>\n<pre style=\"font-style:oblique\">\r\nresult =\r\n\r\n&lt;?xml version=\"1.1\" encoding=\"ASCII\" standalone=\"yes\"?&gt;&lt;AddressBook&gt;&lt;b\/&gt;&lt;a\/&gt;&lt;\/AddressBook&gt;\r\n\r\n<\/pre>\n<p>As you can see, from this code, setting the OutputProperty using <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/javax\/xml\/transform\/OutputKeys.html\" target=\"_blank\">OutputKeys<\/a> allows us to control what shows up in the final XML document. Properties set on <tt>docNode<\/tt> are ignored. In this example I use a StringWriter to get a String form of the XML, but you can use a <a href=\"http:\/\/docs.oracle.com\/javase\/6\/docs\/api\/index.html?java\/io\/FileWriter.html\" target=\"_blank\">FileWriter<\/a> to save it to disk.<\/p>\n<p>I&#8217;ve received some comments that this solution is not the friendly MATLAB code we&#8217;re used to, so I&#8217;ve created enhancement requests to bring some of this functionality into XMLWRITE.<\/p>\n<p>Other posts in the series:<\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.mathworks.com\/community\/2010\/06\/28\/using-xml-in-matlab\/\" title=\"Using XML in MATLAB\" target=\"_blank\">Using XML in MATLAB<\/a><\/li>\n<li><a href=\"https:\/\/blogs.mathworks.com\/community\/2010\/09\/13\/simple-xml-node-creation\/\" title=\"Simple XML Node Creation\" target=\"_blank\">Simple XML Node Creation<\/a><\/li>\n<li><a href=\"https:\/\/blogs.mathworks.com\/community\/2010\/11\/01\/xml-and-matlab-navigating-a-tree\/\" title=\"XML and MATLAB: Navigating a Tree\" target=\"_blank\">XML and MATLAB: Navigating a Tree<\/a><\/li>\n<\/ul>\n<p><span style=\"font-size:10px\"><strong>EDIT 2\/21\/2012: updated javadoc links<\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had a lot of comments in the past few months about MATLAB&#8217;s XML functionality, so I guess it&#8217;s time to provide a few more posts on the matter. Today is part 4 of the series,&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2012\/02\/20\/even-more-xml-and-matlab-controlling-xml-output\/\">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":[203,202,56,201,200],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/655"}],"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=655"}],"version-history":[{"count":22,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/655\/revisions"}],"predecessor-version":[{"id":734,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/655\/revisions\/734"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}