简体   繁体   中英

Parsing a SOAP response using XPath Java

I am new to XPath. I have the following SOAP response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <addParentResponse xmlns="urn:JadeWebServices/NetsuiteCustomer/">
            <addParentResult>Organisation xxxxx already exists - use UpdateParent method instead</addParentResult>
        </addParentResponse>
    </soap:Body>
</soap:Envelope>

Can anyone kindly give me some code which will read the value of "addParentResult"?

Regards, Anirban.

The following xpath should give the desired result :

/soap:Envelope/soap:Body/parentns:addParentResponse/parentns:addParentResult/text()

The reason I added parentns to xpath is that your xml has namespaces and your xpath processor should know about them. But the addParentResponse has no prefix and has default namespace. In this case add a prefix in xpath expression and before doing that tell xpath processor that for the parentns prefix there is a value which is "urn:JadeWebServices/NetsuiteCustomer/" . It is done via a NamespaceContext .

Also be sure to tell the DocumentBuilderFactory that it should be aware of namespaces by using setNamespaceAware( true );

Code in Java would be :

    try 
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document doc = db.parse( new File( "soapResponse.xml" ) );

        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        javax.xml.namespace.NamespaceContext ns = new javax.xml.namespace.NamespaceContext()
        {

            @Override
            public String getNamespaceURI(String prefix) 
            {
                if ( "soap".equals( prefix ) )
                {
                    return "http://schemas.xmlsoap.org/soap/envelope/";
                }
                else if ( "xsi".equals( prefix ) )
                {
                    return "http://www.w3.org/2001/XMLSchema-instance";
                }
                else if ( "xsd".equals( prefix ) )
                {
                    return "http://www.w3.org/2001/XMLSchema";
                }
                else if ( "xml".equals( prefix ) )
                {
                    return javax.xml.XMLConstants.XML_NS_URI;
                }
                else if ( "parentns".equals( prefix ) )
                {
                    return "urn:JadeWebServices/NetsuiteCustomer/";
                }

                return javax.xml.XMLConstants.NULL_NS_URI;
           }

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

            @Override
            public Iterator<?> getPrefixes(String namespaceURI) 
            {
                return null;
            }

        };


         xpath.setNamespaceContext(ns);
         XPathExpression expr = xpath.compile( "/soap:Envelope/soap:Body/parentns:addParentResponse/parentns:addParentResult/text()" );


         Object exprEval = expr.evaluate( doc, XPathConstants.STRING );
         if ( exprEval != null )
         {
            System.out.println( "The text of addParentResult is : " + exprEval );
         }
    }
    catch ( Exception e )
    {
        e.printStackTrace();
    }
 }

To test this code, put your xml in a file called soapResponse.xml at the same level as your java file.

Output from System.out.println() is :

The text of addParentResult is : Organisation xxxxx already exists - use UpdateParent method instead

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