4.0

4.0 | 1 rating Rate this file 14 Downloads (last 30 days) File Size: 13.54 KB File ID: #34711

XMLNode

by Jarrod Rivituso

 

20 Jan 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 (2011b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
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!

18 May 2012 Jarrod Rivituso

Thanks Jeremy! Those are good ideas :)

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
xml xpath java namespace namespaces indexing node Jarrod Rivituso 31 Jan 2012 14:27:21

Contact us at files@mathworks.com