Extracting child nodes data from given node

15.6k Views Asked by At

I want to use DOM4j for parsing xml file in Java.

I have this XML as an example:

<request method="POST" url="/devices/test/planner" body="*">
  <response statusCode="200">
    <header>
      <headerParameters>
        <headerParameter name="Content-Type">
          Content-Type=application/xml
        </headerParameter>
      </headerParameters>
    </header>
    <body>booking created!</body>
  </response>
</request>

Given the request (first node) node, how can I extract the child nodes data?

For example getting the <response> status code, or the <headerParameter> name attributes?

2

There are 2 best solutions below

0
On BEST ANSWER

Assuming you get the "request" node as an Element then you can do something like this:

Element response = (Element) request.elements().get(0);
int statusCode = Integer.parseInt(response.attribute("statusCode"));

If you want to traverse the children recursively then you'll have to write iterative (or recursive) code to visit each element in the list returned by the elements() method.

[Edit] You can also use XPath to extract the specific items you're looking for:

int statusCode = Integer.parseInt(
    request.selectSingleNode("response/@statusCode").getText());
String firstHeaderName =
    request.selectSingleNode(
        "response/headerParameters/headerParameter/@name").getText();
0
On

Extracting child nodes data from given node with dom4j:

1. Put this java code in a file called Main.java:

import java.util.*;
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.*;

class Foo{
    String moo;
    String baz;
}
class Main{
    public static Document parse(String filePath) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(filePath);
        return document;
    }
    public static void main(String[] args){
        try{
            File f = new File("/tmp/myxml.xml");
            Document document = parse(f.toString());    
            List list = document.selectNodes("//penguins/PieHole");
            Foo foo = new Foo();
            Iterator iter=list.iterator();

            while(iter.hasNext()){
                Element element=(Element)iter.next();
                foo.moo = element.selectSingleNode("cupcake").getText();
                foo.baz = element.selectSingleNode("montana").getText();
            }
            System.out.println("foo.moo: " + foo.moo);
            System.out.println("foo.baz: " + foo.baz);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("done");
    }
}

2. Put this in a file called /tmp/myxml.xml:

<?xml version="1.0" encoding="utf-8"?>
<penguins>
  <mars>129</mars>
  <PieHole>
    <cupcake>value inside cupcake</cupcake>
    <montana>value inside montana</montana>
  </PieHole>
</penguins>

2. Put these jar files in a directory called lib in the same directory as Main.java:

dom4j-1.6.1.jar  
jaxen-1.1.1.jar

3. Compile the program and run it from the terminal:

javac -cp .:./lib/* Main.java
java -cp .:./lib/* Main

4. Interpret the output:

eric@defiant ~/code/java/run04 $ javac -cp .:./lib/* Main.java
eric@defiant ~/code/java/run04 $ java -cp .:./lib/* Main
foo.moo: value inside cupcake
foo.baz: value inside montana
done

5. What just happened?

This uses Java version 1.7.0 and imports the dom4j version 1.6.1 library as well as a jaxen 1.1.1 support library. It imports the xml document, created by the user. Then it parses it with the SAXReader into a Document type. It uses the selectNodes(string) method to grab the PieHole xml tag. For every single PieHole xml tag, it will grab the cupcake and montana tags and place them into the Foo class. At the end, it prints what was inside Foo.