File Exchange

image thumbnail

XMLNode

version 1.6.0.1 (14.9 KB) by Jarrod Rivituso
MATLAB class that allows you to use XPath to navigate and extract data from an XML document.

3 Downloads

Updated 01 Sep 2016

View License

Using XMLNode, you can read an XML document into MATLAB as an XMLNode object. This object allows you to navigate and extract data from the XML document using XPath queries.
For example, in the XML document of colors (colors.xml)
<colors>
<color>
<name>red</name>
<hex>FF0000</hex>
</color>
<color>
<name>green</name>
<hex>00FF00</hex>
</color>
<color>
<name>blue</name>
<hex>0000FF</hex>
</color>
</colors>
you could obtain the green hex node using
>> n = XMLNode('colors.xml')
>> n.colors.color(2).hex

or

>> n = XMLNode('colors.xml')
>> n('colors/color[2]/hex')

To extract traditional MATLAB arrays instead of returning XMLNode objects, use {} indexing.

>> n = XMLNode('colors.xml')
>> greenHex = n{'colors/color[2]/hex'}

Supports namespaces and various character encodings.

If you have enhancement suggestions or if you find a bug, please leave a comment :)

Cite As

Jarrod Rivituso (2020). XMLNode (https://www.mathworks.com/matlabcentral/fileexchange/34711-xmlnode), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (12)

Error using XMLNodeImpl (line 119)
Error creating namespace context. Did you run >> XMLNode.initialize ??
I am getting this error message using Matlab 2017b release. Pls, what is the solution?

Error in XMLNode (line 37)
obj@XMLNodeImpl(node,xpath);

Error in comRouter (line 172)
canXmlNode = XMLNode(canFile);

Hi David,

In MATLAB, there is an existing precedent that using parentheses () indexing should return a variable that is the same type of the variable being indexed. In the case of this code, I was trying to keep that behavior.

It gets tricky when we enter xpath expressions like count(), because the result is not a part of the xml document, but instead an actual number.

Perhaps a good enhancement here is to more fully support those types of queries, but only when using {} indexing (which does not have the precedent mentioned earlier).

If you uncomment the code mentioned, do the queries work for you?

Follow up to my previous comment: I did uncomment that code, and I am able to run the types of XPath queries that return numeric values. I have not encountered any problems yet by having this code active.

Russell

Note that running the example with the 2015a PreRelease produces this error:

Error using XMLNodeImpl (line 119)
Error creating namespace context. Did you run >> XMLNode.initialize ??
Error in XMLNode (line 37)
obj@XMLNodeImpl(node,xpath);

Error in xmlNodeExample (line 2)
v = XMLNode('plant_catalog.xml')

which does not occur with the 2014b release.

I was able to parse a big xml file in no time. No loop involved, just a few lines of code.
This is brilliant!

Hi Ferdinando,

The elements in your XML file are all in the 'http://www.nih.gov' namespace, as indicated by the xmlns='http://www.nih.gov' attribute in LidcReadMessage.

When extracting data from an element in a particular namespace using XPath, you must add the namespace prefix. XMLNode automatically finds the prefixes in your document and provides them for you in the command window output when you first open the file.

So, you should be able to use syntax like below to extract your data (note that 'pre1' is the name of the http://www.nih.gov previx that was assigned by XMLNode automatically.

>> n = XMLNode('myFileName.xml')
>> data = n{'//pre1:noduleID'}

Ferdinando

Bryan - sorry for the delay in getting back to you.

I think this may be due to the application not finding the input file specified. I'm not sure whether it is a path issue, or an issue regarding how the file name is being parsed (I found in my testing that certain absolute paths would behave strangely when compiled).

Some things you could try:

1. Try using absolute paths instead of relative paths

2. Try replacing line 80 of XMLNode
fileName = which(node);
with
fileName = node;
to see if this helps (it did for me in my testing)

I was using the "which" function to enable the tool to find any file on the MATLAB path. However, I'm not 100% sure how that plays with the path in a compiled version of the app. I think I'd like to more seriously consider these issues and then upload an update.

Thanks!

Bryan

This has been excellent for use directly within Matlab and EXE compiled apps.

However, I am trying to compile a JAR file using JABUILDER. While running the compiler to create the JAR file, I specify my m file as the main function and class. Within my main function the initialize and additional XMLNode functions are called.

However, when I try to open an XML file in a java application calling the compiled JAR file, I get an error that says content is not allowed in the prolog of the XML file.

I have successfully opened the exact same XML file from MATLAB and a compiled EXE app that both use the XMLNode file. I also opened the XML file directly from Java successfully. The only issue seems to be when I compile into JAR file. Do you know how I can compile XMLNode using JABuilder and still be able to successfully use it?

Thank you in advance!

Great idea. Helpful class.
I also agree with previous comment about the error management. I would like to get an empty object in case of unsuccessful search. Probably a warning (with backtrace on) would be OK in place of error.

Thanks Jeremy! Those are good ideas :)

Very nice class - very helpful. The namespace support in particular is great to find. I would suggest that you add a wrapper function to the class for adding new namespace aliases, rather than jumping to the java expressions. Also, returning an empty array rather than an exception may be more convenient. Perhaps a separate function call could return an exception, or a setting on the class?
Thank you!

Updates

1.6.0.1

Updated license

1.6.0.0

Setting the toolbox flag

1.5.0.0

Fixed copyright statements

1.3.0.0

Updates made based on user comments...

- Empty array now returned when XPath expression is not found in document
- Created an "addNamespacePrefix" method
- Updated command window display

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: Gating-ML