解析SOAP响应返回null
问题描述:
String response = "<?xml version='1.0'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><exch:Response xmlns:exch='http://applicant.ffe.org/exchange/1.0'>...</exch:Response></soap:Body></soap:Envelope>";
DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
org.w3c.dom.Document document = null;
try {
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
document = db.parse(is);
} catch(ParserConfigurationException e){}
catch(SAXException e){}
文档返回null。我尝试过不同的方式传递给InputSource,但文档仍然返回null。任何想法为什么这可能会发生?解析SOAP响应返回null
答
我刚试过,我可以得到元素的名称和值。
try {
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
document = db.parse(is);
System.out.println(document);//here we get null;
System.out.println(document.getNodeName());//here we get document;
for(int i =0 ; i<document.getChildNodes().getLength();i++)
System.out.println(document.getChildNodes().item(i).getChildNodes().item(i).getNodeName());
}
Output :
[#document: null]
document
soap:Body
为了解析SOAPResponse我们可以javax.xml.soap.*
它可能会把你遍历对象XML树。无论如何,我们可能需要解析SOAP Body中的元素。我们可以使用DOM格式解析这些非常简单的方式。
+0
它的工作。我试图检索子节点,它从来没有任何价值。当我调试代码时实现。 – yogsma
可以指定你在文档中的空位置吗? @yogsma – gks