0

I have this response string as XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
  <ns:SRVResponse>
  <ns:Response>
    <ns1:ServiceHeader>
       <ns1:rsHeader>
          <ns1:status>
             <ns1:finStatus>E</ns1:finStatus>
             </ns1:status>
        </ns1:rsHeader>
    </ns1:ServiceHeader>
</ns:Response>
</ns:SRVResponse>
</soapenv:Body>
</soapenv:Envelope>

Im trying to fetch this finStatus tag value. This comes as part of ns1, and some times it comes as ns2. So, I dont want to depend on this. I just need to fetch if the tag has finStatus tag.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                           dbf.setNamespaceAware(true);
                           DocumentBuilder db = dbf.newDocumentBuilder();
                           InputSource is = new InputSource();                                 
                            is.setCharacterStream(new StringReader(strResponse));
                            if(is != null) {
                                Document doc = db.parse(is);                                

                                NodeList idDetails =  doc.getDocumentElement().getElementsByTagNameNS("*", "status");
                                if(idDetails != null) {
                                    int length = idDetails.getLength();
                                    for (int i = 0; i < length; i++) {
                                        if (idDetails.item(i).getNodeType() == Node.ELEMENT_NODE) {
                                            Element el = (Element) idDetails.item(i);
                                            if (el.getNodeName().contains("status")) {
                                                status = getElementTextContent(el, "ns1:finStatus");
                                                System.out
                                                        .println("Status :"+status);
                                            }
                                        }
                                    }
                                }
                            }

Method

public static String getElementTextContent(Element el, String elemTag) {
    String result = "";
    if(el.getElementsByTagName(elemTag) != null) {
        if(el.getElementsByTagName(elemTag).item(0) != null) {
            if(el.getElementsByTagName(elemTag).item(0).getTextContent() != null) {
                result = el.getElementsByTagName(elemTag).item(0).getTextContent();
            } else {
                result = "";
            }
        }
    }
    return result;
}

This is working only because Im passing the tag as ns1:finStatus,

How can I achieve this , not to based on namespace tag.

2 Answers 2

1

1) You are already fetching node without namespace using. So I do not see problem.

2) Use the * search in your second method as well i.e in your getElementTextContent(), use el.getElementsByTagNameNS("*",elemTag).

3) To skip the ns: prefix before passing it to getElementTextContent, Use getLocalName() instead of getNodeName()

Sign up to request clarification or add additional context in comments.

7 Comments

el.getElementsByTagName("*",elemTag), cannot take two parameters as string.
sorry, i meant getElementsByTagNameNS
Yeah got it. Thanks Optional
But I do not see overallStatus in your xml. You can directly fetch this element itself instead of first fetching it's parent. No?
@Sorry Optional, It was a typo. It should be finStatus. Updated in the question
|
1

Install an XPath 2.0 library and do //*:finStatus. Doing this by DOM navigation is just masochism. Either that, or you're being paid for the number of lines of code you write.

2 Comments

No. I'm here to tell you how to solve your problem, not to do your job for you.
Thank you @Michael.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.