我想使用DOM库来解析xml格式的字符串。出于某种原因,我的文档包含空值,我遇到了试图解析它的问题。字符串变量'response'不为空,并且我能够在调试模式下看到字符串。从xml字符串构建DOM文档给我一个空文档
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(response));
Document doc = builder.parse(is);
NodeList nodes = doc.getElementsByTagName("BatchFile");;
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList batchItem = element.getChildNodes();
String uri = batchItem.item(0).getNodeValue();
String id = batchItem.item(1).getNodeValue();
String fqName = batchItem.item(2).getNodeValue();
}
突出过线Document doc = builder.parse(is);
它运行后显示的[#document: null]
结果。
编辑:我设法没有得到一个空文档,但字符串值仍然为空(在代码结束)。我怎么会得到这样的东西
<GetBatchFilesResult>
<BatchFile>
<Uri>uri</Uri>
<ID>id</ID>
<FQName>file.zip</FQName>
</BatchFile>
</GetBatchFilesResult>
这只是'Document.toString'方法 - 并不意味着任何有用的东西。究竟有什么问题? – 2013-03-14 14:25:55
通配符在'getElementsByTagName(“*”);'中不起作用。检查每个'node.items(i)instanceof Element',并进行递归。 – 2013-03-14 14:27:47
文档中似乎没有任何内容。当知道xml字符串中有节点时,doc.getChildNodes根本不显示任何节点。 – zms6445 2013-03-14 14:29:22