检索SOAP响应

时间:2014-02-26 15:19:02

标签: java xml soap

我正在Servlet中编写代码(如下)。这必须在SOAP Response中返回值,但这不显示SOAP文件中的值。我没有任何错误。

我从外面检索xml并在此处插入以便于阅读。因此,我需要能够解析该结果以获得相关的行。

String xmlInput="   <S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
              + " <S:Body>\n"
              + " <ns2:tataResponse xmlns:ns2=\"http://pack/\">\n"
              + " <return>12500</return>\n"
              + "  </ns2:tataResponse>\n"
              + " </S:Body>\n"
              + "</S:Envelope>\n";  
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage(new MimeHeaders(),
                 new ByteArrayInputStream(xmlInput.getBytes(
                                          Charset.forName("UTF-8"))));

SOAPBody body = message.getSOAPBody();
NodeList returnList = body.getElementsByTagName("ns2:tataResponse");

boolean isSucces = false;

for (int k = 0; k < returnList.getLength(); k++) {
   NodeList innerResultList = returnList.item(k).getChildNodes();
   for (int l = 0; l < innerResultList.getLength(); l++) {
      if (innerResultList.item(l).getNodeName().equalsIgnoreCase("return")) {
         isSucces = Integer.valueOf(innerResultList.item(l)
                       .getTextContent().trim()) == 100 ? true : false;
      }
   }
}

if (isSucces) {
   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++) {
          System.out.println(innerList.item(j).getNodeName());
          System.out.println(innerList.item(j).getTextContent());
       }
   }
}

2 个答案:

答案 0 :(得分:0)

永远不会调用测试ifSucces的if子句,因为您将该变量定义为false。您从XML中正确提取了结果,但在此行中它不等于100

isSucces = Integer.valueOf(innerResultList.item(l)
                  .getTextContent().trim()) == 100 ? true : false;

因此ifSuccesfalse

也许如果您将值12500(在您的XML中)替换为100,它将按预期工作。

答案 1 :(得分:0)

问题在于代码未显示。如果您在XML中将12500更改为100,则helderdarocha表示它可以正常工作。见:

import java.io.ByteArrayInputStream;
import javax.xml.soap.*;
import javax.xml.*;
import org.w3c.dom.*;
import java.nio.charset.*;

public class Foo {
public static void main(String[] args) throws Exception {
String xmlInput="   <S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
              + " <S:Body>\n"
              + " <ns2:tataResponse xmlns:ns2=\"http://pack/\">\n"
              + " <return>100</return>\n"
              + "  </ns2:tataResponse>\n"
              + " </S:Body>\n"
              + "</S:Envelope>\n";  
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage(new MimeHeaders(),
                 new java.io.ByteArrayInputStream(xmlInput.getBytes(
                                          Charset.forName("UTF-8"))));

SOAPBody body = message.getSOAPBody();
NodeList returnList = body.getElementsByTagName("ns2:tataResponse");

boolean isSucces = false;

for (int k = 0; k < returnList.getLength(); k++) {
   System.out.println("k: " + k);
   NodeList innerResultList = returnList.item(k).getChildNodes();
   for (int l = 0; l < innerResultList.getLength(); l++) {
   String name = innerResultList.item(l).getNodeName();
   System.out.println("l: " + l + ": -" + name + "-");
      if (name.equalsIgnoreCase("return")) {
         String content = innerResultList.item(l).getTextContent().trim();
         System.out.println("content: " + content);
         isSucces = Integer.valueOf(content) == 100 ? true : false;
      }
   }
}

if (isSucces) {
   NodeList list = body.getElementsByTagName("return");
   for (int i = 0; i < list.getLength(); i++) {
   System.out.println("i: " + i);
       NodeList innerList = list.item(i).getChildNodes();
       for (int j = 0; j < innerList.getLength(); j++) {
   System.out.println("j: " + j);
          System.out.println(innerList.item(j).getNodeName());
          System.out.println(innerList.item(j).getTextContent());
       }
   }
}
}
}