[英]Converting XML to document in java creates null document
我正在尝试从此处(stackoverflow)和其他来源解析示例,这些示例是从java网上从网上下载的xml。
首先,我将xml打包在字符串中:
String xml = getXML(url, logger);
如果我此时打印出xml字符串:
System.out.println("XML " + xml);
我得到了xml的打印输出,因此我假设到目前为止没有故障。 然后,我尝试创建一个可以评估的文档:
InputSource is= new InputSource(new StringReader(xml));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
如果我在这里打印文档:
System.out.println("Doc: " + doc);
我得到:文件:[#document:null]
当我稍后尝试使用Xpath评估表达式时,我得到了java.lang.NullPointerException,并且在尝试获取根的长度时也得到了:
System.out.println("Root length " + rootNode.getLength());
这让我相信文档(以及后来的节点)确实为空。
当我尝试打印输入源或节点时,我得到例如。
输入源:org.xml.sax.InputSource@29453f44
我不知道该怎么解释。
谁能看到我做错了什么或提出前进的方向吗? 提前致谢。
您可能需要另一种方式将文档呈现为字符串。
对于JDOM :
public static String toString(final Document document) {
try {
final ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
final XMLOutputter outp = new XMLOutputter();
outp.output(document, out);
final String string = out.toString("UTF-8");
return string;
}
catch (final Exception e) {
throw new IllegalStateException("Cannot stringify document.", e);
}
}
输出
org.xml.sax.InputSource@29453f44
只是类名+实例的哈希码(在Object类中定义)。 它指示实例的类具有未重写的toString。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.