我试图了解我本可以犯的错误,但无法找到解决方案。
public static Document getXMLFromString(String xml) { org.w3c.dom.Document doc = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder; builder = factory.newDocumentBuilder(); doc = (org.w3c.dom.Document) builder.parse(new InputSource( new StringReader(xml))); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return doc; }
我输入了org.w3c.dom.Document
我在这里称这个方法:
private Node getAuthToken(SOAPMessage responseAuth) throws SOAPException, TransformerException, ParserConfigurationException, IOException, SAXException { String s = indentXML(responseAuth.getSOAPPart().getContent()); Document doc = getXMLFromString(s); NodeList authTokenNodeList = doc.getElementsByTagName("authToken"); return authTokenNodeList.item(0); }
NodeList为空。
在网络上进行研究后,每个人都使用此代码将字符串解析为文档。我没有任何异常,但在调用方法parse()之后,doc的值设置为[#document:null] DeferredDocumentImpl。
我正在使用org.w3c.dom中的所有内容。
xml是一个包含
的字符串<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<context xmlns="urn:zimbra">
<session id="36" type="admin">36</session>
<change token="251"/>
</context>
</soap:Header>
<soap:Body>
<AuthResponse xmlns="urn:zimbraAdmin">
<authToken>...</authToken>
<lifetime>...</lifetime>
<a n="zimbraIsDomainAdminAccount">false</a>
<session id="36" type="admin">36</session>
</AuthResponse>
</soap:Body>
</soap:Envelope>
以下是我在SOAP调用后构建字符串的方法:
String xml = indentXML(responseAuth.getSOAPPart().getContent());
我做错了什么?
这就是我想以一种简单的方式做的事情:
StringBuilder soapResponse = new StringBuilder(... ... ... ); org.w3c.dom.Document doc = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder; builder = factory.newDocumentBuilder(); doc = (org.w3c.dom.Document) builder.parse(new InputSource( new StringReader(soapResponse.toString()))); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } NodeList authTokenNodeList = doc.getElementsByTagName("authToken"); Node n = authTokenNodeList.item(0); String s = n.getNodeValue();
答案 0 :(得分:6)
编辑:看看你的更新代码,我认为这是问题:
String s = n.getNodeValue();
如果您查看Node的文档,您会看到getNodeValue()
被定义为返回null
元素...因此问题。我的示例代码使用了getTextContent()
代替,工作正常。
看起来这只是推迟扩展内存中的对象,直到需要它们为止。
您是否尝试在返回的文档上调用方法而不是仅仅查看调试器?我怀疑一切都按预期工作。如果没有,请发一个简短但完整的程序,表明它行为不端。 (请注意,在您提供的示例中,您甚至没有显示builder
的设置方式,也没有使用factory
。)
import org.w3c.dom.*;
import org.xml.sax.*;
import java.text.*;
import java.util.*;
import javax.xml.parsers.*;
import java.io.*;
import com.google.common.base.*;
import com.google.common.io.*;
public class Test {
public static void main(String[] args) throws Exception {
String xml = Files.toString(new File("test.xml"), Charsets.UTF_8);
Node node = getAuthToken(xml);
System.out.println(node.getTextContent());
}
private static Node getAuthToken(String xml) throws Exception {
Document doc = getXMLFromString(xml);
NodeList authTokenNodeList = doc.getElementsByTagName("authToken");
return authTokenNodeList.item(0);
}
public static Document getXMLFromString(String xml) throws Exception {
Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xml)));
return doc;
}
}