X-Received: by 10.43.92.73 with SMTP id bp9mr10286051icc.26.1397215953962; Fri, 11 Apr 2014 04:32:33 -0700 (PDT) X-Received: by 10.140.23.39 with SMTP id 36mr34044qgo.14.1397215953930; Fri, 11 Apr 2014 04:32:33 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!ur14no6230322igb.0!news-out.google.com!dz10ni6221qab.1!nntp.google.com!m5no833649qaj.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Fri, 11 Apr 2014 04:32:33 -0700 (PDT) In-Reply-To: <533f6f84$0$307$14726298@news.sunsite.dk> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=196.203.59.237; posting-account=QE_RBAoAAABnLH2Y0MV4Na_K-mkfWqdf NNTP-Posting-Host: 196.203.59.237 References: <533f6f84$0$307$14726298@news.sunsite.dk> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <37957ad3-d000-474b-a1ec-e154c52bc150@googlegroups.com> Subject: Re: Parsing Soap Response in java From: Emna Sabri Injection-Date: Fri, 11 Apr 2014 11:32:33 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: csiph.com comp.lang.java.programmer:28026 Le samedi 5 avril 2014 03:50:42 UTC+1, Arne Vajh=F8j a =E9crit=A0: > On 4/4/2014 6:07 AM, emmna.90@gmail.com wrote: >=20 > > Hi,i need to parse a soap response from an xml fileb in java to get >=20 > > some specific values of it .For example ,i need to have the value v1 o= f >=20 > > string parameter in operationname1 and v2 of string parameter of >=20 > > operationname2.i tried with some tuto in the net but it doesn't work >=20 > > for me. >=20 > > >=20 > > Here is the soap response.xml. >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > v1 >=20 > > value >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > v2 >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 > > >=20 >=20 >=20 > 0) That is not the SOAP result as it is not well formed XML. >=20 >=20 >=20 > I will assume it looks like: >=20 >=20 >=20 > >=20 > >=20 > >=20 > >=20 > >=20 > >=20 > >=20 > =20 > modifier=3D"modify1"> >=20 > >=20 > =20 > name=3D"n1">v1 >=20 > =20 > name=3D"d1">value >=20 > >=20 > >=20 > =20 > modifier=3D"modify2"> >=20 > >=20 > =20 > name=3D"c1">v2 >=20 > >=20 > >=20 > >=20 > >=20 > >=20 > >=20 > >=20 > >=20 >=20 >=20 > 1) If you are process XML when you are doing SOAP then you are most >=20 > like doing it wrong. You should be generating stub code from the >=20 > WSDL and just make a method call and not worry about XML at all. >=20 >=20 >=20 > 2) If you really need to parse the response manually, then I will >=20 > recommend using XPath. >=20 >=20 >=20 > Example reading the above XML from a disk file soap.xml: >=20 >=20 >=20 > import java.io.FileReader; >=20 > import java.io.IOException; >=20 >=20 >=20 > import javax.xml.parsers.DocumentBuilder; >=20 > import javax.xml.parsers.DocumentBuilderFactory; >=20 > import javax.xml.parsers.ParserConfigurationException; >=20 > import javax.xml.xpath.XPath; >=20 > import javax.xml.xpath.XPathConstants; >=20 > import javax.xml.xpath.XPathExpressionException; >=20 > import javax.xml.xpath.XPathFactory; >=20 >=20 >=20 > import org.w3c.dom.Document; >=20 > import org.w3c.dom.Node; >=20 > import org.xml.sax.InputSource; >=20 > import org.xml.sax.SAXException; >=20 >=20 >=20 > public class ManualSoapParse { >=20 > public static void main(String[] args) throws=20 >=20 > ParserConfigurationException, SAXException, IOException,=20 >=20 > XPathExpressionException { >=20 > DocumentBuilderFactory dbf =3D DocumentBuilderFactory.newInstance(); >=20 > dbf.setNamespaceAware(false); >=20 > DocumentBuilder db =3D dbf.newDocumentBuilder(); >=20 > Document doc =3D db.parse(new InputSource(new=20 >=20 > FileReader("/work/soap.xml"))); >=20 > XPath xpath =3D XPathFactory.newInstance().newXPath(); >=20 > Node n1 =3D=20 >=20 > (Node)xpath.evaluate("//Envelope/Body/CommandResponseData/CommandResult/T= ransactionResult/OperationResult/Operation[@name=3D'operationname1']/Parame= terList/StringParameter[@name=3D'n1']/text()",=20 >=20 > doc.getDocumentElement(), XPathConstants.NODE); >=20 > System.out.println(n1.getNodeValue()); >=20 > Node c1 =3D=20 >=20 > (Node)xpath.evaluate("//Envelope/Body/CommandResponseData/CommandResult/T= ransactionResult/OperationResult/Operation[@name=3D'operationname2']/Parame= terList/StringParameter[@name=3D'c1']/text()",=20 >=20 > doc.getDocumentElement(), XPathConstants.NODE); >=20 > System.out.println(c1.getNodeValue()); >=20 > } >=20 > } >=20 >=20 >=20 > Note that I cheated and ignored namespaces. You can handle namespaces if >=20 > you need to or want to. >=20 >=20 >=20 > Arne hi Arne ,thank you for your response ,i used the 2nd issue to manually pars= e the soap response and it gives me errors in this line (System.out.println= (n1.getNodeValue());)=20 I have even added the namespace context by adding this code Namespace= Context ctx =3D new NamespaceContext() { public String getNamespaceURI(String prefix) { String uri; if (prefix.equals("ns1")) { uri =3D "uri of ns1"; } else { uri =3D 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 .