4.0

4.0 | 2 ratings Rate this file 11 Downloads (last 30 days) File Size: 13.6 KB File ID: #34711

XMLNode

by Jarrod Rivituso

 

20 Jan 2012 (Updated 29 Aug 2012)

MATLAB class that allows you to use XPath to navigate and extract data from an XML document.

| Watch this File

File Information
Description

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 :)

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File  
Everyone's Tags
xml xpath java namespace namespaces indexing node
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
29 Jun 2012 Agostino De Marco

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.

18 May 2012 Jarrod Rivituso

Thanks Jeremy! Those are good ideas :)

17 May 2012 Jeremy Turpin

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
29 Aug 2012

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

Contact us