gpt4 book ai didi

xml - 获取文档为 null [#document : null] After parsing XML in java using DocumentBuilder

转载 作者:数据小太阳 更新时间:2023-10-29 01:44:39 30 4
gpt4 key购买 nike

解析文档后我得到空值,即使文档包含数据。这是我的代码,我已将所有验证设置为 false。

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();

domFactory.setNamespaceAware(false); // never forget this!
domFactory.setCoalescing(false);
domFactory.setValidating(false);
domFactory.setFeature("http://xml.org/sax/features/namespaces", false);
domFactory.setFeature("http://xml.org/sax/features/validation", false);
domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
domFactory.setFeature("http://apache.org/xml/features/allow-java-encodings",
true);



DocumentBuilder builder = domFactory.newDocumentBuilder();

builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId)
throws SAXException, java.io.IOException {
if (publicId.equals("--myDTDpublicID--"))
// this deactivates the open office DTD
return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
else return null;
}
});


Document doc = null;
URL url = new URL(urlStr);
URLConnection urlc = url.openConnection();



doc = builder.parse(urlc.getInputStream());
System.out.println("doc:" + doc.toString());

响应如下:

doc:[#document: null]

为什么?我是否缺少一些验证?

最佳答案

[#document: null] 只是您的 doc 实例的 toString,它不会输出您的整个 XML 文档。

实例本身不为空,您可能可以毫无问题地继续处理。

关于xml - 获取文档为 null [#document : null] After parsing XML in java using DocumentBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694600/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com