Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parsing Soap Response in java

9,102 views
Skip to first unread message

emmn...@gmail.com

unread,
Apr 4, 2014, 6:07:23 AM4/4/14
to
Hi,i need to parse a soap response from an xml fileb in java to get some specific values of it .For example ,i need to have the value v1 of string parameter in operationname1 and v2 of string parameter of operationname2.i tried with some tuto in the net but it doesn't work for me .I will be very thankful if you can help me .
Here is the soap response.xml.


<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="...">
<soapenv:Body>
<ns1:CommandResponseData xmlns:ns1="...">
<ns1:CommandResult>
<ns1:TransactionResult>
<ns1:OperationResult>
<ns1:Operation name="operationname1" modifier="modify1">
<ns1:ParameterList>
<ns1:StringParameter name="n1">v1</ns1:StringParameter>
<ns1:DateTimeParametername="d1">value</ns1:DateTimeParameter>
</ns1:ParameterList>
</ns1:Operation>

</ns1:Operation>
<ns1:Operation name="operationname2" modifier="modify2">
<ns1:ParameterList>
<ns1:StringParameter name="c1">v2</ns1:StringParameter>
</ns1:ParameterList>
</ns1:Operation>
</ns1:OperationResult>
</ns1:TransactionResult>
</ns1:CommandResult>
</ns1:CommandResponseData>
</soapenv:Body>
</soapenv:Envelope>

Jeff Higgins

unread,
Apr 4, 2014, 3:20:25 PM4/4/14
to
On 04/04/2014 06:07 AM, emmn...@gmail.com wrote:
some tuto in the net
<http://docs.oracle.com/javase/tutorial/jaxp/index.html>


Roedy Green

unread,
Apr 4, 2014, 6:28:32 PM4/4/14
to
On Fri, 4 Apr 2014 03:07:23 -0700 (PDT), emmn...@gmail.com wrote,
quoted or indirectly quoted someone who said :

>Hi,i need to parse a soap response from an xml fileb in java to get some s=
>pecific values of it .For example ,i need to have the value v1 of string pa=
>rameter in operationname1 and v2 of string parameter of operationname2.i tr=
>ied with some tuto in the net but it doesn't work for me .I will be very t=
>hankful if you can help me .
>Here is the soap response.xml.

I parse the soap responses from Amazon that tells me which books they
have in stock.

You need a Jax-compatible descriptor of the soap records. It then
generates you Java sourcecode to parse the incoming stream.

You then navigate the tree to pick out what you want.

see http://mindprod.com/jax.html

Amazon provides a descriptor file
AWSECommerceService.wsdl

that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"
targetNamespace="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"
targetNamespace="http://webservices.amazon.com/AWSECommerceService/2011-08-01"
elementFormDefault="qualified">
<xs:element name="Bin">
<xs:complexType>
<xs:sequence>
...
--
Roedy Green Canadian Mind Products http://mindprod.com
"The question of whether machines can think is about as relevant
as the question of whether submarines can swim"
~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

Arne Vajhøj

unread,
Apr 4, 2014, 10:50:42 PM4/4/14
to
On 4/4/2014 6:07 AM, emmn...@gmail.com wrote:
> Hi,i need to parse a soap response from an xml fileb in java to get
> some specific values of it .For example ,i need to have the value v1 of
> string parameter in operationname1 and v2 of string parameter of
> operationname2.i tried with some tuto in the net but it doesn't work
> for me.
>
0) That is not the SOAP result as it is not well formed XML.

I will assume it looks like:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="...">
<soapenv:Body>
<ns1:CommandResponseData xmlns:ns1="...">
<ns1:CommandResult>
<ns1:TransactionResult>
<ns1:OperationResult>
<ns1:Operation name="operationname1"
modifier="modify1">
<ns1:ParameterList>
<ns1:StringParameter
name="n1">v1</ns1:StringParameter>
<ns1:DateTimeParameter
name="d1">value</ns1:DateTimeParameter>
</ns1:ParameterList>
</ns1:Operation>
<ns1:Operation name="operationname2"
modifier="modify2">
<ns1:ParameterList>
<ns1:StringParameter
name="c1">v2</ns1:StringParameter>
</ns1:ParameterList>
</ns1:Operation>
</ns1:OperationResult>
</ns1:TransactionResult>
</ns1:CommandResult>
</ns1:CommandResponseData>
</soapenv:Body>
</soapenv:Envelope>

1) If you are process XML when you are doing SOAP then you are most
like doing it wrong. You should be generating stub code from the
WSDL and just make a method call and not worry about XML at all.

2) If you really need to parse the response manually, then I will
recommend using XPath.

Example reading the above XML from a disk file soap.xml:

import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ManualSoapParse {
public static void main(String[] args) throws
ParserConfigurationException, SAXException, IOException,
XPathExpressionException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new
FileReader("/work/soap.xml")));
XPath xpath = XPathFactory.newInstance().newXPath();
Node n1 =
(Node)xpath.evaluate("//Envelope/Body/CommandResponseData/CommandResult/TransactionResult/OperationResult/Operation[@name='operationname1']/ParameterList/StringParameter[@name='n1']/text()",
doc.getDocumentElement(), XPathConstants.NODE);
System.out.println(n1.getNodeValue());
Node c1 =
(Node)xpath.evaluate("//Envelope/Body/CommandResponseData/CommandResult/TransactionResult/OperationResult/Operation[@name='operationname2']/ParameterList/StringParameter[@name='c1']/text()",
doc.getDocumentElement(), XPathConstants.NODE);
System.out.println(c1.getNodeValue());
}
}

Note that I cheated and ignored namespaces. You can handle namespaces if
you need to or want to.

Arne

Emna Sabri

unread,
Apr 11, 2014, 7:32:33 AM4/11/14
to
hi Arne ,thank you for your response ,i used the 2nd issue to manually parse the soap response and it gives me errors in this line (System.out.println(n1.getNodeValue());)
I have even added the namespace context by adding this code NamespaceContext ctx = new NamespaceContext() {

public String getNamespaceURI(String prefix) {
String uri;
if (prefix.equals("ns1")) {
uri = "uri of ns1";
} else {
uri = null;
}
return uri;
}

public String getPrefix(String namespaceURI) {
throw new UnsupportedOperationException("Not supported yet.");
}

public Iterator getPrefixes(String namespaceURI) {
throw new UnsupportedOperationException("Not supported yet.");
}
};
and still haven't succeed to get the values .
Thanks in advance for your help .

Silvio

unread,
Apr 11, 2014, 7:49:54 AM4/11/14
to
What you want would be ease to do with DOM plus maybe some XPath if the
XML where valid, which it is not.

Silvio

Emna Sabri

unread,
Apr 11, 2014, 8:31:39 AM4/11/14
to
Sorry but how can i parse the soap response if it isn't with xml parsing ?

Silvio

unread,
Apr 11, 2014, 12:05:59 PM4/11/14
to
It would be with XML parsing IF it where actual XML. What you have
provided is not valid XML and can therefore not be parsed with an XML
parser. Are you sure you posted an actual sample of what you are trying
to parse?

Arne Vajhøj

unread,
Apr 11, 2014, 9:03:36 PM4/11/14
to
On 4/11/2014 7:32 AM, Emna Sabri wrote:
> i used the 2nd issue to manually parse the soap response and it gives me errors in this line (System.out.println(n1.getNodeValue());)

Do you expect us to be able to guess what the problem is without you
telling us what "gives me errors" covers?

Arne

Arne Vajhøj

unread,
Apr 11, 2014, 9:05:24 PM4/11/14
to
On 4/11/2014 8:31 AM, Emna Sabri wrote:
> Sorry but how can i parse the soap response if it isn't with xml parsing ?

If you have a strong desire for some unmaintainable code, then there are
lots of possibilities in String indexOf and String substring.

Arne


lipska the kat

unread,
Apr 12, 2014, 4:56:55 AM4/12/14
to
On 04/04/14 11:07, emmn...@gmail.com wrote:

There are two problems with this XML
Try fixing them and then parse again.

> <?xml version='1.0' encoding='utf-8'?>
> <soapenv:Envelope xmlns:soapenv="...">
> <soapenv:Body>
> <ns1:CommandResponseData xmlns:ns1="...">
> <ns1:CommandResult>
> <ns1:TransactionResult>
> <ns1:OperationResult>
> <ns1:Operation name="operationname1" modifier="modify1">
> <ns1:ParameterList>
> <ns1:StringParameter name="n1">v1</ns1:StringParameter>
----------> problem ----->
<ns1:DateTimeParametername="d1">value</ns1:DateTimeParameter>

> </ns1:ParameterList>
> </ns1:Operation>
>
----------> problem ------> </ns1:Operation>
> <ns1:Operation name="operationname2" modifier="modify2">
> <ns1:ParameterList>
> <ns1:StringParameter name="c1">v2</ns1:StringParameter>
> </ns1:ParameterList>
> </ns1:Operation>
> </ns1:OperationResult>
> </ns1:TransactionResult>
> </ns1:CommandResult>
> </ns1:CommandResponseData>
> </soapenv:Body>
> </soapenv:Envelope>


--
lipska the kat - treacherous feline.
Proudly nominated for IPOTY 2014 - LIGAF
GNU/Linux user #560883 - linuxcounter.net

Emna Sabri

unread,
Apr 17, 2014, 4:42:41 PM4/17/14
to
Hi ,i solved the problem :) your response helps me a lot ,in fact the fault was when copying values .Thanks
0 new messages