我试图理解我可能犯的错误,但无法找到解决方案 .
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为空 .
在网上研究之后,每个人都使用此代码将字符串解析为Document . 我没有任何异常但是在调用方法parse()之后,doc的值被设置为[#document:null] DeferredDocumentImpl .
我正在使用org.w3c.dom中的所有内容 .
xml是包含的字符串
36
...
...
false
36
以下是我在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();