Reputation: 33
I have a soap web service which returns given response:
<soap:Envelope xmlns:soap="">
<soap:Body>
<ns2:getRegisterValuesResponse xmlns:ns2="">
<return>
<id>11931</id>
<value>0</value>
</return>
<return>
<id>11946</id>
<value>0</value>
</return>
<return>
<id>11961</id>
<value>0</value>
</return>
</ns2:getRegisterValuesResponse>
</soap:Body>
</soap:Envelope>
How do i retrieve given integers inside a java method?
Here's my method. The idea is to get the database update every X minutes with given ids and values.
public class RegisterLog implements Job {
public void execute(final JobExecutionContext ctx)
throws JobExecutionException {
SimulatorSOAPClientSAAJ sc=new SimulatorSOAPClientSAAJ();
SOAPMessage msg = sc.sOAPConnect();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
msg.writeTo(out);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String strMsg = new String(out.toByteArray());
System.out.println(strMsg);
Upvotes: 1
Views: 61
Reputation: 129
Using DOM XML PARSER http://www.w3schools.com/dom/default.asp
Imports
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
Code
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
Document doc = dBuilder.parse(bais);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("return");
for (int i=0;i<nList.getLength();i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element curElement = (Element) nNode;
int id = Integer.parseInt(curElement.getElementsByTagName("id").item(0).getTextContent());
String value = curElement.getElementsByTagName("value").item(0).getTextContent();
}
}
Upvotes: 1