Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28026
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2014-04-11 04:32 -0700 |
| References | <bd5fa8aa-5b67-4999-adb3-e45be51f3e11@googlegroups.com> <533f6f84$0$307$14726298@news.sunsite.dk> |
| Message-ID | <37957ad3-d000-474b-a1ec-e154c52bc150@googlegroups.com> (permalink) |
| Subject | Re: Parsing Soap Response in java |
| From | Emna Sabri <emmna.90@gmail.com> |
Le samedi 5 avril 2014 03:50:42 UTC+1, Arne Vajhøj a écrit :
> On 4/4/2014 6:07 AM, emmna.90@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.
>
> >
>
> > 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>
>
>
>
> 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
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 .
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Parsing Soap Response in java emmna.90@gmail.com - 2014-04-04 03:07 -0700
Re: Parsing Soap Response in java Jeff Higgins <jeff@invalid.invalid> - 2014-04-04 15:20 -0400
Re: Parsing Soap Response in java Roedy Green <see_website@mindprod.com.invalid> - 2014-04-04 15:28 -0700
Re: Parsing Soap Response in java Arne Vajhøj <arne@vajhoej.dk> - 2014-04-04 22:50 -0400
Re: Parsing Soap Response in java Emna Sabri <emmna.90@gmail.com> - 2014-04-11 04:32 -0700
Re: Parsing Soap Response in java Arne Vajhøj <arne@vajhoej.dk> - 2014-04-11 21:03 -0400
Re: Parsing Soap Response in java Silvio <silvio@internet.com> - 2014-04-11 13:49 +0200
Re: Parsing Soap Response in java Emna Sabri <emmna.90@gmail.com> - 2014-04-11 05:31 -0700
Re: Parsing Soap Response in java Silvio <silvio@internet.com> - 2014-04-11 18:05 +0200
Re: Parsing Soap Response in java Arne Vajhøj <arne@vajhoej.dk> - 2014-04-11 21:05 -0400
Re: Parsing Soap Response in java Emna Sabri <emmna.90@gmail.com> - 2014-04-17 13:42 -0700
Re: Parsing Soap Response in java lipska the kat <lipskathekat@example.com> - 2014-04-12 09:56 +0100
csiph-web