In the following example, I demonstrate how to get all the node values from an XML based on the XPath in MATLAB and Java. XPath stands for XML Path Language and is a query language for selecting nodes in an XML document. The XPath language is based on a tree representation of the XML document. As I am going to show in this post, XPath provides a straightforward way to navigate the tree and select nodes according to various criteria.
Let’s consider the following example XML file (book.xml):
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <author>Ralls, Kim Inside</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk101"> <author>Ralls, Kim bk101</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> </catalog>
What I would like to achieve is to get all the authors names for the books which have id
of bk101
. This can be easily achieved by using xpath
:
mport javax.xml.xpath.* factory = XPathFactory.newInstance; xpath = factory.newXPath; fXML=xmlread('books.xml'); authors = xpath.evaluate('catalog/book[@id="bk101"]/author', fXML, XPathConstants.NODESET); for i=0:authors.getLength-1 disp(authors.item(i).getFirstChild().getNodeValue()) end
If you are using the expression in a, for example, for
or while
loop, I recommend compiling it first to prevent performance drawbacks => The evaluate
function will call the compile
function each time. After updating the code, by including the compile
function, the whole example would be:
import javax.xml.xpath.* factory = XPathFactory.newInstance; xpath = factory.newXPath; fXML=xmlread('books.xml'); expression = xpath.compile('catalog/book[@id="bk101"]/author'); authors = expression.evaluate(fXML, XPathConstants.NODESET); for i=0:authors.getLength-1 disp(authors.item(i).getFirstChild().getNodeValue()) end