我有一个soap web服务,它返回给定的响应:
<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>
如何在java方法中检索给定的整数?
这是我的方法。我们的想法是每隔X分钟使用给定的ID和值来更新数据库。
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);
答案 0 :(得分:1)
使用DOM XML PARSER http://www.w3schools.com/dom/default.asp
<强>进口强>
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
<强>代码强>
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();
}
}