0

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?

miqbal
  • 2,213
  • 3
  • 27
  • 35

1 Answers1

1

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
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you. Actually I'm searching for access to particular element directly easy way. But it helped. – miqbal Dec 21 '12 at 01:12