I have a soap response that look like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:dcrown">
<SOAP-ENV:Body>
<ns1:jj_syncResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
<return xsi:type="tns:jj_sync">
<verification xsi:type="xsd:bool">1</verification>
<status xsi:type="xsd:bool">1</status>
</return>
</ns1:jj_syncResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
how to read the value of
verification
status
and assign it into variable
I tried to modify from following code but no success:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class testSoapRead {
private static List<Element> elements(NodeList nodes) {
List<Element> result = new ArrayList<Element>(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element)
result.add((Element)node);
}
return result;
}
private static Element named(Element elem, String name) {
if (!elem.getNodeName().equals(name))
throw new IllegalArgumentException("Expected " + name + ", got " + elem.getNodeName());
return elem;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, SOAPException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \r\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n" +
" <soap:Body>\r\n" +
" <ns1:jj_syncResponse xmlns:ns1=\"http://schemas.xmlsoap.org/soap/envelope/\"> \r\n" +
" <return xsi:type=\"tns:jj_sync\">\r\n" +
" <verification xsi:type=\"xsd:bool\">1</verification>\r\n" +
" <status xsi:type=\"xsd:bool\">1</status>\r\n" +
" </return>\r\n" +
" </ns1:jj_syncResponse>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>\r\n" +
"";
//System.out.println("xml : "+xml);
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage msg = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xml.getBytes(Charset.forName("UTF-8"))));
msg.saveChanges();
SOAPBody soapBody = msg.getSOAPBody();
for (Element response : elements(soapBody.getElementsByTagName("jj_syncResponse"))) {
List<Element> children = elements(result.getChildNodes());
Element resultData = named(children.get(0), "return");
List<Element> resultDataChildren = elements(resultData.getChildNodes());
boolean validated = Boolean.getBoolean(named(resultDataChildren.get(0), "verification").getTextContent());
boolean status = Boolean.getBoolean(named(resultDataChildren.get(0), "status").getTextContent());
System.out.println("validated : "+validated+" &&& status : "+status);
}
}
}
I found a solution and here is the working code using for loop:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class testSoapRead {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception, SOAPException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \r\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n" +
" <soap:Body>\r\n" +
" <ns1:jj_syncResponse xmlns:ns1=\"http://schemas.xmlsoap.org/soap/envelope/\"> \r\n" +
" <return xsi:type=\"tns:jj_sync\">\r\n" +
" <verification xsi:type=\"xsd:bool\">yyyy</verification>\r\n" +
" <status xsi:type=\"xsd:bool\">nnnn</status>\r\n" +
" </return>\r\n" +
" </ns1:jj_syncResponse>\r\n" +
" </soap:Body>\r\n" +
"</soap:Envelope>\r\n" +
"";
//System.out.println("xml : "+xml);
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage(new MimeHeaders(),
new java.io.ByteArrayInputStream(xml.getBytes(
Charset.forName("UTF-8"))));
SOAPBody body = message.getSOAPBody();
NodeList returnList = body.getElementsByTagName("ns1:jj_syncResponse");
String strVerification = "";
String strStatus = "";
NodeList list = body.getElementsByTagName("return");
for (int i = 0; i < list.getLength(); i++) {
NodeList innerList = list.item(i).getChildNodes();
for (int j = 0; j < innerList.getLength(); j++) {
if (innerList.item(j).getNodeName() == "verification"){
strVerification = innerList.item(j).getTextContent();
}
if (innerList.item(j).getNodeName() == "status"){
strStatus = innerList.item(j).getTextContent();
}
}
}
System.out.println("strVerification : "+strVerification);
System.out.println("strStatus : "+strStatus);
}
}
Result is:
--------------------Configuration: soapcron - JDK version 1.6.0_23 --------------------
strVerification : yyyy
strStatus : nnnn
Process completed.
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.