<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>XML &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 01 Feb 2017 09:52:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>Parsing XML strings</title>
		<link>https://undocumentedmatlab.com/articles/parsing-xml-strings?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=parsing-xml-strings</link>
					<comments>https://undocumentedmatlab.com/articles/parsing-xml-strings#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 01 Feb 2017 09:52:45 +0000</pubDate>
				<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented feature]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6838</guid>

					<description><![CDATA[<p>Matlab's xmlread function cannot process XML data directly, but this can easily be overcome.  </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/parsing-xml-strings">Parsing XML strings</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/parsing-mlint-code-analyzer-output" rel="bookmark" title="Parsing mlint (Code Analyzer) output">Parsing mlint (Code Analyzer) output </a> <small>The Matlab Code Analyzer (mlint) has a lot of undocumented functionality just waiting to be used. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/file-deletion-memory-leaks-performance" rel="bookmark" title="File deletion memory leaks, performance">File deletion memory leaks, performance </a> <small>Matlab's delete function leaks memory and is also slower than the equivalent Java function. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround" rel="bookmark" title="Matlab compiler bug and workaround">Matlab compiler bug and workaround </a> <small>Both the Matlab compiler and the publish function have errors when parsing block-comments in Matlab m-code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-xml-functionality" rel="bookmark" title="Undocumented XML functionality">Undocumented XML functionality </a> <small>Matlab's built-in XML-processing functions have several undocumented features that can be used by Java-savvy users...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>I have recently consulted in a project where data was provided in XML strings and needed to be parsed in Matlab memory in an efficient manner (in other words, as quickly as possible). Now granted, XML is rather inefficient in storing data (JSON would be much better for this, for example). But I had to work with the given situation, and that required processing the XML.<br />
I basically had two main alternatives:</p>
<ul>
<li>I could either create a dedicated string-parsing function that searches for a particular pattern within the XML string, or</li>
<li>I could use a standard XML-parsing library to create the XML model and then parse its nodes</li>
</ul>
<p>The first alternative is quite error-prone, since it relies on the exact format of the data in the XML. Since the same data can be represented in multiple equivalent XML ways, making the string-parsing function robust as well as efficient would be challenging. I was <del>lazy</del> expedient, so I chose the second alternative.<br />
Unfortunately, Matlab&#8217;s <i><b>xmlread</b></i> function only accepts input filenames (of *.xml files), it cannot directly parse XML strings. Yummy!<br />
The obvious and simple solution is to simply write the XML string into a temporary *.xml file, read it with <i><b>xmlread</b></i>, and then delete the temp file:</p>
<pre lang="matlab">
% Store the XML data in a temp *.xml file
filename = [tempname '.xml'];
fid = fopen(filename,'Wt');
fwrite(fid,xmlString);
fclose(fid);
% Read the file into an XML model object
xmlTreeObject = xmlread(filename);
% Delete the temp file
delete(filename);
% Parse the XML model object
...
</pre>
<p>This works well and we could move on with our short lives. But cases such as this, where a built-in function seems to have a silly limitation, really fire up the investigative reporter in me. I decided to drill into <i><b>xmlread</b></i> to discover why it couldn&#8217;t parse XML strings directly in memory, without requiring costly file I/O. It turns out that <i><b>xmlread</b></i> accepts not just file <i>names</i> as input, but also Java object references (specifically, <a href="http://docs.oracle.com/javase/7/docs/api/java/io/File.html" rel="nofollow" target="_blank"><code>java.io.File</code></a>, <a href="http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html" rel="nofollow" target="_blank"><code>java.io.InputStream</code></a> or <a href="http://docs.oracle.com/javase/7/docs/api/org/xml/sax/InputSource.html" rel="nofollow" target="_blank"><code>org.xml.sax.InputSource</code></a>). In fact, there are quite a few other inputs that we could use, to specify a validation parser etc. &#8211; <a href="/articles/undocumented-xml-functionality" target="_blank">I wrote about this briefly</a> back in 2009 (along with other similar semi-documented input altermatives in <i><b>xmlwrite</b></i> and <i><b>xslt</b></i>).<br />
<span id="more-6838"></span><br />
In our case, we could simply send <i><b>xmlread</b></i> as input a <code><a href="http://docs.oracle.com/javase/7/docs/api/java/io/StringBufferInputStream.html" rel="nofollow" target="_blank">java.io.StringBufferInputStream</a>(xmlString)</code> object (which is an instance of <code>java.io.InputStream</code>) or <code>org.xml.sax.InputSource(java.io.StringReader(xmlString))</code>:</p>
<pre lang="matlab">
% Read the xml string directly into an XML model object
inputObject = java.io.StringBufferInputStream(xmlString);                % alternative #1
inputObject = org.xml.sax.InputSource(java.io.StringReader(xmlString));  % alternative #2
xmlTreeObject = xmlread(inputObject);
% Parse the XML model object
...
</pre>
<p>If we don&#8217;t want to depend on undocumented functionality (which might break in some future release, although it has remained unchanged for at least the past decade), and in order to improve performance even further by passing <i><b>xmlread</b></i>&#8216;s internal validity checks and processing, we can use <i><b>xmlread</b></i>&#8216;s core functionality to parse our XML string directly. We can add a fallback to the standard (fully-documented) functionality, just in case something goes wrong (which is good practice whenever using any undocumented functionality):</p>
<pre lang="matlab">
try
    % The following avoids the need for file I/O:
    inputObject = java.io.StringBufferInputStream(xmlString);  % or: org.xml.sax.InputSource(java.io.StringReader(xmlString))
    try
        % Parse the input data directly using xmlread's core functionality
        parserFactory = javaMethod('newInstance','javax.xml.parsers.DocumentBuilderFactory');
        p = javaMethod('newDocumentBuilder',parserFactory);
        xmlTreeObject = p.parse(inputObject);
    catch
        % Use xmlread's semi-documented inputObject input feature
        xmlTreeObject = xmlread(inputObject);
    end
catch
    % Fallback to standard xmlread usage, using a temporary XML file:
    % Store the XML data in a temp *.xml file
    filename = [tempname '.xml'];
    fid = fopen(filename,'Wt');
    fwrite(fid,xmlString);
    fclose(fid);
    % Read the file into an XML model object
    xmlTreeObject = xmlread(filename);
    % Delete the temp file
    delete(filename);
end
% Parse the XML model object
...
</pre>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/parsing-xml-strings">Parsing XML strings</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/parsing-mlint-code-analyzer-output" rel="bookmark" title="Parsing mlint (Code Analyzer) output">Parsing mlint (Code Analyzer) output </a> <small>The Matlab Code Analyzer (mlint) has a lot of undocumented functionality just waiting to be used. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/file-deletion-memory-leaks-performance" rel="bookmark" title="File deletion memory leaks, performance">File deletion memory leaks, performance </a> <small>Matlab's delete function leaks memory and is also slower than the equivalent Java function. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-compiler-bug-and-workaround" rel="bookmark" title="Matlab compiler bug and workaround">Matlab compiler bug and workaround </a> <small>Both the Matlab compiler and the publish function have errors when parsing block-comments in Matlab m-code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-xml-functionality" rel="bookmark" title="Undocumented XML functionality">Undocumented XML functionality </a> <small>Matlab's built-in XML-processing functions have several undocumented features that can be used by Java-savvy users...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/parsing-xml-strings/feed</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
		<item>
		<title>Undocumented XML functionality</title>
		<link>https://undocumentedmatlab.com/articles/undocumented-xml-functionality?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=undocumented-xml-functionality</link>
					<comments>https://undocumentedmatlab.com/articles/undocumented-xml-functionality#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 19 Nov 2009 01:12:06 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented feature]]></category>
		<category><![CDATA[Stock Matlab function]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=751</guid>

					<description><![CDATA[<p>Matlab's built-in XML-processing functions have several undocumented features that can be used by Java-savvy users</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/undocumented-xml-functionality">Undocumented XML functionality</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/parsing-xml-strings" rel="bookmark" title="Parsing XML strings">Parsing XML strings </a> <small>Matlab's xmlread function cannot process XML data directly, but this can easily be overcome. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/hidden-hg2-plot-functionality" rel="bookmark" title="Accessing hidden HG2 plot functionality">Accessing hidden HG2 plot functionality </a> <small>In HG2, some of the plot functionality is hidden in undocumented properties. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/xlsread-functionality-change-in-r2012a" rel="bookmark" title="xlsread functionality change in R2012a">xlsread functionality change in R2012a </a> <small>The functionality of the xlsread function has changed without documentation or warning in the R2012a release. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/ismembc-undocumented-helper-function" rel="bookmark" title="ismembc &#8211; undocumented helper function">ismembc &#8211; undocumented helper function </a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab&#8217;s built-in XML-processing functions have several undocumented features that can be used by Java-savvy users. We should note that the entire XML-support functionality in Matlab is java-based. I understand that some Matlab users have a general aversion to Java, some even going as far as to disable it using the -nojvm startup option. But if you disable Java, Matlab&#8217;s XML functions will simply not work. Matlab&#8217;s own <a target="_blank" rel="nofollow" href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xmlread.html">documentation</a> points users to Sun&#8217;s official Java website for explanations of how to use the XML functionality (the link in the Matlab docpage is dead &#8211; the correct link should probably be <a target="_blank" rel="nofollow" href="https://jaxp-sources.dev.java.net/nonav/docs/api/">https://jaxp-sources.dev.java.net/nonav/docs/api/</a>, but Sun keeps changing its website so this link could also be dead soon&#8230;).<br />
Using the full Java XML parsing (<a target="_blank" rel="nofollow" href="https://jaxp.dev.java.net/">JAXP</a>) functionality is admittedly quite intimidating for the uninitiated, but extremely powerful once you understand how all the pieces fit together. Over the years, several interesting utilities were submitted to the Matlab File Exchange that simplify this intimidating post-processing. See for example <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/3074-xml-parsing-tools">XML parsing tools</a>, the extremely popular <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/4278-xml-toolbox">XML Toolbox</a> and <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/12907-xmliotools">xml_io_tools</a>, the recent <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/18253-xml-data-import">XML data import</a> and perhaps a dozen other utilities.<br />
Each of Matlab&#8217;s main built-in XML-processing functions, <b><i>xmlread</i></b>, <b><i>xmlwrite</i></b> and <b><i>xslt</i></b> has an internal set of undocumented and unsupported functionalities, which builds on their internal Java implementation. As far as I could tell, these unsupported functionalities were supported at least as early as Matlab 7.2 (R2006a), and possibly even on earlier releases. For the benefit of the Java and/or JAXP -speakers out there (it will probably not help any others), I list Matlab&#8217;s internal description of these unsupported functionalities, annotated with API hyperlinks. These description (sans the links) can be seen by simply editing the m file, as in (the R2008a variant is described below):</p>
<pre lang="matlab">edit xmlread</pre>
<h3><i>xmlread</i></h3>
<p></p>
<pre lang="matlab">function [parseResult,p] = xmlread(fileName,varargin)</pre>
<ul>
<li>FILENAME can also be an <a target="_blank" rel="nofollow" href="https://jaxp-sources.dev.java.net/nonav/docs/api/org/xml/sax/InputSource.html">InputSource</a>, <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/io/File.html">File</a>, or <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/io/InputStream.html">InputStream</a> object
</li>
<li>DOMNODE = XMLREAD(FILENAME,&#8230;,P,&#8230;) where P is a <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html">DocumentBuilder</a> object
</li>
<li>DOMNODE = XMLREAD(FILENAME,&#8230;,&#8217;-validating&#8217;,&#8230;) will create a validating parser if one was not provided.
</li>
<li>DOMNODE = XMLREAD(FILENAME,&#8230;,ER,&#8230;) where ER is an <a target="_blank" rel="nofollow" href="https://jaxp-sources.dev.java.net/nonav/docs/api/org/xml/sax/EntityResolver.html">EntityResolver</a> will set the EntityResolver before parsing
</li>
<li>DOMNODE = XMLREAD(FILENAME,&#8230;,EH,&#8230;) where EH is an <a target="_blank" rel="nofollow" href="https://jaxp-sources.dev.java.net/nonav/docs/api/org/xml/sax/ErrorHandler.html">ErrorHandler</a> will set the ErrorHandler before parsing
</li>
<li>[DOMNODE,P] = XMLREAD(FILENAME,&#8230;) will return a parser suitable for passing back to XMLREAD for future parses.
</li>
</ul>
<h3><i>xmlwrite</i></h3>
<p></p>
<pre lang="matlab">
function xmlwrite(FILENAME,DOMNODE);
function str = xmlwrite(DOMNODE);
function str = xmlwrite(SOURCE);
</pre>
<ul>
<li>FILENAME can also be a <a target="_blank" rel="nofollow" href="http://en.wikipedia.org/wiki/Uniform_Resource_Name">URN</a>, <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/io/OutputStream.html">java.io.OutputStream</a> or <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/io/Writer.html">java.io.Writer</a> object
</li>
<li>SOURCE can also be a SAX <a target="_blank" rel="nofollow" href="https://jaxp-sources.dev.java.net/nonav/docs/api/org/xml/sax/InputSource.html">InputSource</a>, <a target="_blank" rel="nofollow" href="https://jaxp-sources.dev.java.net/nonav/docs/api/javax/xml/transform/Source.html">JAXP Source</a>, <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/io/InputStream.html">InputStream</a>, or <a target="_blank" rel="nofollow" href="http://java.sun.com/javase/6/docs/api/java/io/Reader.html">Reader</a> object</li>
</ul>
<h3><i>xslt</i></h3>
<p></p>
<pre lang="matlab">function [xResultURI,xProcessor] = xslt(SOURCE,STYLE,DEST,varargin)</pre>
<ul>
<li>SOURCE can also be a <a target="_blank" rel="nofollow" href="http://xerces.apache.org/xerces2-j/javadocs/xni/org/apache/xerces/xni/parser/XMLInputSource.html">XSLTInputSource</a>
</li>
<li>STYLE can also be a StylesheetRoot or XSLTInputSource
</li>
<li>DEST can also be an XSLTResultTarget.  Note that RESULT may be empty in this case since it may not be possible to determine a URL. If STYLE is absent or empty, the function uses the stylesheet named in the xml-stylesheet processing instruction in the SOURCE XML file.  (This does not always work)
</li>
<li>There is also an entirely undocumented feature: passing a &#8216;-tostring&#8217; input argument transforms the inputs into a displayed text segment, rather than into a displayed URI; the transformed text is returned in the xResultURI output argument.</li>
</ul>
<p>Note: internal comments within the Matlab code seem to indicate that XSLT is <a target="_blank" rel="nofollow" href="http://saxon.sourceforge.net/">SAXON</a>-based, so interested users might use SAXON&#8217;s documentation for accessing additional XSLT-related features/capabilities (also see <a href="https://www.mathworks.com/matlabcentral/answers/264420-why-does-matlab-still-use-the-saxon-6-5-5-xslt-and-xquery-processor-when-saxon9-jar-has-already-been" rel="nofollow" target="_blank">this related thread</a>).</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/undocumented-xml-functionality">Undocumented XML functionality</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/parsing-xml-strings" rel="bookmark" title="Parsing XML strings">Parsing XML strings </a> <small>Matlab's xmlread function cannot process XML data directly, but this can easily be overcome. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/hidden-hg2-plot-functionality" rel="bookmark" title="Accessing hidden HG2 plot functionality">Accessing hidden HG2 plot functionality </a> <small>In HG2, some of the plot functionality is hidden in undocumented properties. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/xlsread-functionality-change-in-r2012a" rel="bookmark" title="xlsread functionality change in R2012a">xlsread functionality change in R2012a </a> <small>The functionality of the xlsread function has changed without documentation or warning in the R2012a release. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/ismembc-undocumented-helper-function" rel="bookmark" title="ismembc &#8211; undocumented helper function">ismembc &#8211; undocumented helper function </a> <small>Matlab has several undocumented internal helper functions that can be useful on their own in some cases. This post presents the ismembc function....</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/undocumented-xml-functionality/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
