- Undocumented Matlab - https://undocumentedmatlab.com -

Undocumented XML functionality

Posted By Yair Altman On November 18, 2009 | 6 Comments

Matlab’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’s XML functions will simply not work. Matlab’s own documentation [1] points users to Sun’s official Java website for explanations of how to use the XML functionality (the link in the Matlab docpage is dead – the correct link should probably be https://jaxp-sources.dev.java.net/nonav/docs/api/ [2], but Sun keeps changing its website so this link could also be dead soon…).
Using the full Java XML parsing (JAXP [3]) 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 XML parsing tools [4], the extremely popular XML Toolbox [5] and xml_io_tools [6], the recent XML data import [7] and perhaps a dozen other utilities.
Each of Matlab’s main built-in XML-processing functions, xmlread, xmlwrite and xslt 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’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):

edit xmlread


function [parseResult,p] = xmlread(fileName,varargin)
  • FILENAME can also be an InputSource [8], File [9], or InputStream [10] object
  • DOMNODE = XMLREAD(FILENAME,…,P,…) where P is a DocumentBuilder [11] object
  • DOMNODE = XMLREAD(FILENAME,…,’-validating’,…) will create a validating parser if one was not provided.
  • DOMNODE = XMLREAD(FILENAME,…,ER,…) where ER is an EntityResolver [12] will set the EntityResolver before parsing
  • DOMNODE = XMLREAD(FILENAME,…,EH,…) where EH is an ErrorHandler [13] will set the ErrorHandler before parsing
  • [DOMNODE,P] = XMLREAD(FILENAME,…) will return a parser suitable for passing back to XMLREAD for future parses.


function xmlwrite(FILENAME,DOMNODE);
function str = xmlwrite(DOMNODE);
function str = xmlwrite(SOURCE);


function [xResultURI,xProcessor] = xslt(SOURCE,STYLE,DEST,varargin)
  • SOURCE can also be a XSLTInputSource [19]
  • STYLE can also be a StylesheetRoot or XSLTInputSource
  • 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)
  • There is also an entirely undocumented feature: passing a ‘-tostring’ 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.

Note: internal comments within the Matlab code seem to indicate that XSLT is SAXON [20]-based, so interested users might use SAXON’s documentation for accessing additional XSLT-related features/capabilities (also see this related thread [21]).

Categories: Java, Low risk of breaking in future versions, Semi-documented feature, Stock Matlab function

6 Comments (Open | Close)

6 Comments To "Undocumented XML functionality"

#1 Comment By Donn Shull On November 20, 2009 @ 10:58

Hi Yair,

It seems that many people follow an unusal convention for the format of their xml files. Even The MathWork’s info.xml files don’t following the standard indented form for xml files. A quick way to “pretty print” an xml file is to use saveXML method of the DOM node ie:

x = xmlread(which('info.xml'));

Thanks for the good work,


#2 Comment By Yair Altman On November 21, 2009 @ 09:36

Here’s a nicer “pretty-print”, which uses the undocumented InputSource input argument format of xlswrite:


#3 Comment By James Myatt On January 30, 2012 @ 14:46

xmlread does a great job of parsing an XML file, but I’ve found that actually extracting data from the DOM hierarchy is pretty slow. For example, the parseXML function in the xmlread help takes about 250 times longer than the call to xmlread itself and vast majority of that time is spent interrogating the DOM objects.

Do you have any suggestions?

#4 Comment By Yair Altman On January 30, 2012 @ 15:42

@James – DOM is normally used for small XML models; SAX is usually better for large models that can be processed sequentially. There are numerous SAX parsers available online that you can use in Matlab. Perhaps the most widely used open-source XML parser, which includes support fro both SAX and DOM, is Xerces, which is already pre-bundled in Matlab (take a look at the %matlabroot%/java/jarext/ folder), so you can use it in Matlab out-of-the-box. Other well-known XML support packages, namely Xalan and Saxon, are also pre-bundled.

#5 Comment By James Myatt On February 1, 2012 @ 05:10

@Yair, That certainly sounds like a better idea, but I’m not sure how to proceed. Could you indicate how to modify the parseXML function from the xmlread help so that it used the SAX parser rather than the DOM returned by xmlread? Or am I on the wrong track?

#6 Comment By Yair Altman On February 1, 2012 @ 17:47

@James – I don’t have an immediate answer for you, it requires some investigation. This sounds like a good idea for a future article. If you cannot wait for this article to appear, you could contact me by email (link at the top right of this page) to discuss a short consulting gig.

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/undocumented-xml-functionality

URLs in this post:

[1] documentation: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/xmlread.html

[2] https://jaxp-sources.dev.java.net/nonav/docs/api/: https://jaxp-sources.dev.java.net/nonav/docs/api/

[3] JAXP: https://jaxp.dev.java.net/

[4] XML parsing tools: http://www.mathworks.com/matlabcentral/fileexchange/3074-xml-parsing-tools

[5] XML Toolbox: http://www.mathworks.com/matlabcentral/fileexchange/4278-xml-toolbox

[6] xml_io_tools: http://www.mathworks.com/matlabcentral/fileexchange/12907-xmliotools

[7] XML data import: http://www.mathworks.com/matlabcentral/fileexchange/18253-xml-data-import

[8] InputSource: https://jaxp-sources.dev.java.net/nonav/docs/api/org/xml/sax/InputSource.html

[9] File: http://java.sun.com/javase/6/docs/api/java/io/File.html

[10] InputStream: http://java.sun.com/javase/6/docs/api/java/io/InputStream.html

[11] DocumentBuilder: http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilder.html

[12] EntityResolver: https://jaxp-sources.dev.java.net/nonav/docs/api/org/xml/sax/EntityResolver.html

[13] ErrorHandler: https://jaxp-sources.dev.java.net/nonav/docs/api/org/xml/sax/ErrorHandler.html

[14] URN: http://en.wikipedia.org/wiki/Uniform_Resource_Name

[15] java.io.OutputStream: http://java.sun.com/javase/6/docs/api/java/io/OutputStream.html

[16] java.io.Writer: http://java.sun.com/javase/6/docs/api/java/io/Writer.html

[17] JAXP Source: https://jaxp-sources.dev.java.net/nonav/docs/api/javax/xml/transform/Source.html

[18] Reader: http://java.sun.com/javase/6/docs/api/java/io/Reader.html

[19] XSLTInputSource: http://xerces.apache.org/xerces2-j/javadocs/xni/org/apache/xerces/xni/parser/XMLInputSource.html

[20] SAXON: http://saxon.sourceforge.net/

[21] this related thread: 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

[22] Parsing XML strings : https://undocumentedmatlab.com/articles/parsing-xml-strings

[23] Accessing hidden HG2 plot functionality : https://undocumentedmatlab.com/articles/hidden-hg2-plot-functionality

[24] xlsread functionality change in R2012a : https://undocumentedmatlab.com/articles/xlsread-functionality-change-in-r2012a

[25] ismembc – undocumented helper function : https://undocumentedmatlab.com/articles/ismembc-undocumented-helper-function

[26] cellfun – undocumented performance boost : https://undocumentedmatlab.com/articles/cellfun-undocumented-performance-boost

[27] Types of undocumented Matlab aspects : https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.