简体   繁体   中英

Unable to parse SOAP response elements using XParser in Java due to colon present in nodes

I am trying to parse one node from SOAP response through Xparser but I am unable to reach the specific node due to : present in nodes. Following is my XML :

<?xml version='1.0' encoding='ISO-8859-1'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
    <ns:processTransactionResponse  xmlns:ns="http://service.tpe.tbms.suntecgroup.com" 
        xmlns:ax212="http://rmi.java/xsd"
        xmlns:ax221="http://request.service.commons.tbms.suntecgroup.com/xsd"
        xmlns:ax213="http://io.java/xsd" 
        xmlns:ax222="http://utilities.service.commons.tbms.suntecgroup.com/xsd"
        mlns:ax225="http://response.service.commons.tbms.suntecgroup.com/xsd"
        xmlns:ax217="http://request.service.tpe.tbms.suntecgroup.com/xsd" 
        xmlns:ax219="http://response.service.tpe.tbms.suntecgroup.com/xsd">
                    <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                    xsi:type="ax225:ProcessResponse">
                        <ax225:abstractServiceObjects xsi:type="ax222:AbstractServiceObject">
                            <ax222:fieldId>2201</ax222:fieldId>
                            <ax222:format xsi:nil="true" />
                            <ax222:operator>=</ax222:operator>
                            <ax222:type>1</ax222:type>
                            <ax222:value>Customer ID was Active</ax222:value>

                        </ax225:abstractServiceObjects>

                    </ns:return></ns:processTransactionResponse>
            </soapenv:Body>
    </soapenv:Envelope>

and the JAVA code :

 File reposnseFile  = new File("J:/SOAP/response.txt");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(false);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = (Document) db.parse(reposnseFile);
            XPath xpath = XPathFactory.newInstance().newXPath();
            xpath.setNamespaceContext(new NamespaceContext() {

                @Override
                public Iterator getPrefixes(String arg0) {
                    return null;
                }

                @Override
                public String getPrefix(String arg0) {
                    return null;
                }

                @Override
                public String getNamespaceURI(String arg0) {
                    if("soapenv".equals(arg0)) {
                        return "http://schemas.xmlsoap.org/soap/envelope/nsreturn/ax225abstractServiceObjects/ax222value";                      
                    }
                    return null;
                }
            });
            XPathExpression expr = xpath.compile("//soapenv:Envelope/soapenv:Body/ns:processTransactionResponse/ns:return/ax222:value");
            System.out.println((String) expr.evaluate(doc, XPathConstants.STRING));
    }

Expected Output : Customer ID was Active (Value of a node <ax222:value> ). Your help will be appreciable.

First: your XML has an error its xmlns:ax225

Second, because of the namespace, use that, it works

XPath xpath = XPathFactory.newInstance().newXPath();
expression="//*[local-name()='value']/text()";
String result = xpath.compile(expression).evaluate(document);
System.out.println("RESULT="+result);

thanks to : XPath with namespace in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM