3

I have a sample response:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <trk:TrackResponse xmlns:trk="somens1">
            <common:Response xmlns:common="somens2">
                <common:ResponseStatus>
                    <common:Code>1</common:Code>
                    <common:Description>Success</common:Description>
                </common:ResponseStatus>
                <common:TransactionReference>
                    <common:CustomerContext>Sample Response</common:CustomerContext>
                </common:TransactionReference>
            </common:Response>

        </trk:TrackResponse>
    </soapenv:Body>
</soapenv:Envelope>

I want to remove the Envelope, Header and Body tags and use only the xml beginning from TrackResponse.

I am trying to traverse the response using jdom.

   Element body = jdom.getRootElement().getChild("Body", Namespace.getNamespace("http://schemas.xmlsoap.org/soap/envelope/"));

I actually need a jdom Document object retrieved from the jdom Element starting from TrackResponse.

Solution or better alternatives?

Anurag
  • 723
  • 3
  • 13
  • 31

1 Answers1

1

With your body content retrieved with:

Element body = jdom.getRootElement().getChild("Body", Namespace.getNamespace("http://schemas.xmlsoap.org/soap/envelope/"));

Do:

Namespace trk = Namespace.getNamespace("somens1");
Element response = body.getChild("TrackResponse", trk);
response.detach();
Document doc = new Document(response);

This will create a new document with the trk prefixed namespace and the TrackResponse at the root. The detach() is required when moving an element from one location (parent) to another.

rolfl
  • 17,539
  • 7
  • 42
  • 76