Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing XML using XPath in Java

Tags:

java

xpath

Say I have the following XML file:

<?xml version="1.0" encoding="utf-8"?>
<venues>
  <group type="Nearby">
    <venue>
      <id>222307</id>
      <name>Union Chapel</name>
      <primarycategory>
        <id>78967</id>
        <fullpathname>Arts &amp; Entertainment:Music Venue</fullpathname>
        <nodename>Music Venue</nodename>
        <iconurl>http://foursquare.com/img/categories/arts_entertainment/musicvenue.png</iconurl>
      </primarycategory>
      <address>Compton Ave</address>
      <city>Islington</city>
      <state>Greater London</state>
      <zip>N1 2XD</zip>
      <verified>false</verified>
      <geolat>51.5439732</geolat>
      <geolong>-0.1020908</geolong>
      <stats>
        <herenow>0</herenow>
      </stats>
      <phone>02073594019</phone>
      <distance>33</distance>
    </venue>

.............

and my code is the following:

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//venue/*");

    Object result = expr.evaluate(document, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    //System.out.println(nodes.getLength());

    Venue ven = new Venue();

    for (int i = 0; i < nodes.getLength(); i++) {
        String nodeName = nodes.item(i).getNodeName();
        String nodeValue = nodes.item(i).getNodeValue();


        if (nodeName.equals("id")){
            ven = new Venue();
            if (nodeValue != null)
                ven.id = Integer.parseInt(nodeValue);
            System.out.println(ven.id);
        }

        if (nodeName.equals("name")){
            ven.name = nodeValue;
            System.out.println(ven.name);
        }

        if (nodeName.equals("address")){
            ven.address = nodeValue;
            System.out.println(ven.address);
        }

How can I do all of this in one for loop for efficiency? Otherwise for every attribute in the xml that I want to extract I need to create a for loop for each one of them

like image 327
aherlambang Avatar asked Dec 29 '22 05:12

aherlambang


1 Answers

If you use this as your xpath:

//venue/*

You'll get all the child nodes of venue. You can then iterate over this and do a big if else on the node name's and assign them as needed.

Like this:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//venue/*");

Object result = expr.evaluate(document, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item( i );
            String nodeName = node.getNodeName();
            String nodeValue = node.getChildNodes().item( 0 ).getNodeValue();


            if( nodeName.equals( "name" ) ) {
                        name = nodeValue;
            } 
            else if( nodeName.equals( "address" ) ) {
                        address = nodeValue;
            } // ... the rest goes here
}

If you don't want to iterate over all child elements you could do something like this:

    XPathExpression expr = xpath.compile( "//venue" );

    Object result = expr.evaluate( document, XPathConstants.NODESET );
    NodeList nodes = (NodeList)result;
    for( int i = 0; i < nodes.getLength(); i++ ) {
        Node node = nodes.item( i );
        NodeList venueChildNodes = node.getChildNodes();

        String id = venueChildNodes.item( 1 ).getChildNodes().item( 0 ).getNodeValue();
        System.out.println( "id: " + id );

        String name = venueChildNodes.item( 3 ).getChildNodes().item( 0 ).getNodeValue();
        System.out.println( "name: " + name );

        String address = venueChildNodes.item( 7 ).getChildNodes().item( 0 ).getNodeValue();
        System.out.println( "address: " + address );
    }

Where you get all venue nodes and then map it's children. Though, this approach would require a fairly consistent xml structure. Though, something like this seems safest to me:

    XPathExpression expr = xpath.compile( "//venue" );

    Object result = expr.evaluate( document, XPathConstants.NODESET );
    NodeList nodes = (NodeList)result;
    for( int i = 0; i < nodes.getLength(); i++ ) {
        Node node = nodes.item( i );
        NodeList venueChildNodes = node.getChildNodes();

        String address = null;
        String name = null;

        for( int j = 0; j < venueChildNodes.getLength(); j++ ) {
            Node item = venueChildNodes.item( j );
            String nodeName = item.getNodeName();

            if ( nodeName.equals( "address" ) ) {
                address = item.getChildNodes().item( 0 ).getNodeValue();
            }

            if ( nodeName.equals( "name" ) ) {
                name = item.getChildNodes().item( 0 ).getNodeValue();
            }
        }

        System.out.println( "address: " + address );
        System.out.println( "name: " + name );
    }
like image 105
javamonkey79 Avatar answered Dec 30 '22 18:12

javamonkey79