miqbal
miqbal

Reputation: 2227

Parsing SOAP response from file with JDOM

I'm trying to parse SOAP response from file. This is out.xml

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <response xmlns="http://tempuri.org/">
    <result>
    <config>...</config>
    <config>...</config>
    <config>...</config>
    </result>
    </response>
    </soap:Body>
    </soap:Envelope>

This is code with jdom:

SAXBuilder builder = new SAXBuilder();
try {

   Document document = builder.build( new File("out.xml"));
   Element root = document.getRootElement();
   Namespace ns = Namespace.getNamespace("http://tempuri.org/");
   List r = root.getChildren("config", ns);
   System.out.println(r.size());

}

Why does this output 0?

Upvotes: 0

Views: 2175

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599706

JDOM's getChildren method is documented as this (emphasis mine):

This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.

See the original here.

Your call to getRootElement puts you onto soap:Envelope, which doesn't have any config child nodes.

To get around this, you can either:

  1. call getChildren multiple times to navigate through the soap:Body, response and result elements
  2. call getDescendants to get an iterator that does traverse the entire hierarchy and not just one level

Upvotes: 1

Related Questions