7

I need to parse an XML string with MATLAB (caution: without file I/O, so I don't want to write the string to a file and then read them). I'm receiving the strings from an HTTP connection and the parsing should be very fast. I'm mostly concerned about reading the values of certain tags in the entire string

The net is full of death threats about parsing XML with regexp so I didn't want to get into that just yet. I know MATLAB has seamless java integration but I'm not very java savvy. Is there a quick way to get certain values from XML very very rapidly?

For example I want to get the 'volume' information from this string below and write this to a variable.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>


<root>
  <volume>256</volume>
  <length>0</length>
  <time>0</time>
  <state>stop</state>
  ....
1
  • Are you using windows? If so, then I can offer you a .NET solution. Commented Feb 28, 2012 at 22:42

3 Answers 3

7

For what it's worth, below is the Matlab executable Java code to perform the required task, without writing to an intermediate file:

%An XML formatted string
strXml = [...
    '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>' char(10)...
    '<root>' char(10) ...
    '  <volume>256</volume>' char(10) ...
    '  <length>0</length>' char(10) ...
    '  <time>0</time>' char(10) ...
    '  <state>stop</state>' char(10) ...
    '</root>' ];

%"simple" java code to create a document from said string
xmlDocument = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder.parse(java.io.StringBufferInputStream(strXml));

%"intuitive" methods to explore the xmlDocument
nodeList = xmlDocument.getElementsByTagName('volume');
numberOfNodes = nodeList.getLength();

firstNode = nodeList.item(0);
firstNodeContent = firstNode.getTextContent;

disp(firstNodeContent);  %Returns '256'

As an alternative, if your application allows it, consider passing the URL directly into your XML parser. Untested java code is below, but that probably also opens up the Matlab built-in xslt function as well.

xmlDocument = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder.parse('URL_AS_A_STRING_HERE');

Documentation here. Start at the "javax.xml.parsers" package.

1

There's an entire class of functions for dealing with xml, including xmlread and xmlwrite. Those should be pretty useful for your problem.

1
  • 3
    But he did say explicitly he does not want to deal with files Commented Feb 28, 2012 at 22:42
1

I am not familiar with Matlab's APIs at all, but I would point out that using the DOM method outlined by Pursuit will take the most time/memory if you only want specific values out of the XML stream you are getting back over the HTTP connection.

While STAX will give you the fastest parsing approach in Java, using the API can be unwieldy especially if you are not that familiar with Java. You could use SJXP which is an extremely thin abstraction ontop of STAX parsing in Java (disclaimer: I am the author) that allows you to define paths to the elements you want, then you give the parser a stream (your HTTP stream in this case) and it pulls out all the values for you.

As an example, let's say you wanted the /root/state and /root/volume values out of the examples XML you posted, the actual Java would look something like this:

// Create /root/state rule
IRule stateRule = new DefaultRule(Type.CHARACTER, "/root/state") {
    @Override
    public void handleParsedCharacters(XMLParser parser, String text, Object userObject) {
        System.out.println("State is: " + text);
    }
}

// Create /root/volume rule
IRule volRule = new DefaultRule(Type.CHARACTER, "/state/volume") {
    @Override
    public void handleParsedCharacters(XMLParser parser, String text, Object userObject) {
        System.out.println("Volume is: " + text);
    }
}

// Create the parser with the given rules
XMLParser parser = new XMLParser(stateRule, volRule);

You can do all of that initialization on program start then at some point later when you are processing the stream from your HTTP connection, you would do something like:

parser.parser(httpConnection.getOutputStream());

or the like; then all of your handler code you defined in your rules will get called as the parser runs through the stream of characters from the HTTP connection.

As I mentioned I am not familiar with Matlab and don't know the proper ways to "Matlab-i-fy" this code, but it looks like from the first example you can more or less just use the Java APIs directly in which case this solution will both be faster and use significantly less memory for parsing if that is important than the DOM approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.