Reputation: 949
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.
Upvotes: 1
Views: 5915
Reputation: 14228
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
Upvotes: 5