获取文档为空 [#document: null] 使用 DocumentBuilder 在 java 中解析 XML 后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5694600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:39:41  来源:igfitidea点击:

Getting document as null [#document: null] After parsing XML in java using DocumentBuilder

xmlnulldocument

提问by Pradeep

After parsing the documengt I am getting null, even though the document contains data. Here is my code, I have set all validations to false.

解析文档后我得到空值,即使文档包含数据。这是我的代码,我已将所有验证设置为 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());

The response comes as :

响应如下:

doc:[#document: null]

Why? Am I missing some validation?

为什么?我错过了一些验证吗?

回答by wjans

[#document: null]is just the toString of your docinstance, it doesn't output your whole XML document.

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

The instance itself is not null, you can probably continue your processing without a problem.

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

回答by Pradeep

you should add the static modifier to your Document object, I had the same problem and after the parsing action all non-static objects were simply referring to null. Making it static somehow forces java to keep the reference to the created object during the parse action in your variable.

您应该将静态修饰符添加到您的 Document 对象中,我遇到了同样的问题,并且在解析操作之后,所有非静态对象都只是引用了 null。使其静态以某种方式强制 java 在变量的解析操作期间保留对创建对象的引用。