[英]Converting XML to document in java creates null document
I'm trying to parse xml, downloaded from the web, in java, following examples from here (stackoverflow) and other sources. 我正在尝试从此处(stackoverflow)和其他来源解析示例,这些示例是从java网上从网上下载的xml。
First I pack the xml in a string: 首先,我将xml打包在字符串中:
String xml = getXML(url, logger);
If I printout the xml string at this point: 如果我此时打印出xml字符串:
System.out.println("XML " + xml);
I get a printout of the xml so I'm assuming there is no fault up to this point. 我得到了xml的打印输出,因此我假设到目前为止没有故障。 Then I try to create a document that I can evaluate:
然后,我尝试创建一个可以评估的文档:
InputSource is= new InputSource(new StringReader(xml));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);
If I print out the document here: 如果我在这里打印文档:
System.out.println("Doc: " + doc);
I get: Doc: [#document: null] 我得到:文件:[#document:null]
When I later try to evaluate expressions with Xpath I get java.lang.NullPointerException and also when just trying to get the length of the root: 当我稍后尝试使用Xpath评估表达式时,我得到了java.lang.NullPointerException,并且在尝试获取根的长度时也得到了:
System.out.println("Root length " + rootNode.getLength());
which leaves me to believe the document (and later the node) is truly null. 这让我相信文档(以及后来的节点)确实为空。
When I try to print out the Input Source or the Node I get eg. 当我尝试打印输入源或节点时,我得到例如。
Input Source: org.xml.sax.InputSource@29453f44 输入源:org.xml.sax.InputSource@29453f44
which I don't know how to interpret. 我不知道该怎么解释。
Can any one see what I've done wrong or suggest a way forward? 谁能看到我做错了什么或提出前进的方向吗? Thanks in advance.
提前致谢。
You may need another way to render the document as a string. 您可能需要另一种方式将文档呈现为字符串。
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);
}
}
The output 输出
org.xml.sax.InputSource@29453f44
simply is the class name + the hash code of the instance (as defined in the Object class). 只是类名+实例的哈希码(在Object类中定义)。 It indicates that the class of the instance has toString not overridden.
它指示实例的类具有未重写的toString。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.