Processing XML data in Matlab

Many web-services return information in the form of an XML-formatted string. To process it, one can first convert this string into an XmlDOM object and then use its methods to get the data.

Converting XML string into object in Matlab.

import org.xml.sax.InputSource
import javax.xml.parsers.*
iS = InputSource();
xml = xmlread(iS);

Some useful DOM methods:

  • Get a list of nodes (tags): nodes = xml.getElementsByTagName(nodename);
    nodename is what stands after < and after </
    E.g. photos = xml.getElementsByTagName('photos');
  • Get the number of nodes found: nodes.getLength
    The first index is 0.
  • Get node attribute value like id in <user id=”12037949754@N01″>: nodes.item(0).getAttribute(attrName);
    E.g. char(photos.item(i).getAttribute('total'))
  • Get node value of a text-node like <username>Bees</username>: char(nodes.item(0).getTextContent)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s