【问题标题】:org.w3c.dom.DOMException: DOM005 Wrong document in Java while using Xcercesorg.w3c.dom.DOMException: DOM005 Wrong document in Java while using Xcerces
【发布时间】:2018-11-18 01:36:54
【问题描述】:

我正在尝试使用 DocumentTypeImpl using Xcerces of JAVA 。

这是一段代码

Document htmlDoc = new MyHTMLDocumentImpl();

DocumentType docType = new DocumentTypeImpl(null, "html",
          "-//W3C//DTD XHTML 1.0 Strict//EN",
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
System.out.println("MAYYYYY docType"+docType);
System.out.println("JUNEEEE htmlDoc"+htmlDoc)
htmlDoc.appendChild(docType);

但是在“appendChild”功能时出现以下错误 我面临以下错误

org.w3c.dom.DOMException: DOM005 Wrong document
        at org.apache.xerces.dom.ParentNode.internalInsertBefore(ParentNode.java:393)
        at org.apache.xerces.dom.ParentNode.insertBefore(ParentNode.java:325)
        at org.apache.xerces.dom.DocumentImpl.insertBefore(DocumentImpl.java:410)
        at org.apache.xerces.dom.NodeImpl.appendChild(NodeImpl.java:226)
        at main.document.MyHTMLDocumentImpl.makeBasicHtmlDoc(MyHTMLDocumentImpl.java:34)
        at main.parse.QtUiFileParser.parse(QtUiFileParser.java:52)
        at main.WebApplicationView.main(WebApplicationView.java:58)

请支持

【问题讨论】:

    标签: java dom xerces


    【解决方案1】:

    不要使用new DocumentTypeImpl(...)

    使用htmlDoc.getImplementation().createDocumentType(...)

    【讨论】:

    • 对不起,我是 Java 新手。我已经添加了这样的内容。 DocumentType docType = document.getImplementation().createDocumentType(null, "html", 但是出现错误。找不到符号 "document"
    • 对不起,这是htmlDoc,而不是document。我会更正答案
    • 这是我修改后的代码 Document htmlDoc = new MyHTMLDocumentImpl(); DocumentType docType = htmlDoc.getImplementation().createDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"); System.out.println("aprrr docType"+docType) ; htmlDoc.appendChild(docType); 得到与上面相同的错误。
    • 你应该看看 DocumentBuilderFactory 和 DocumentBuilder 而不是 MyHTMLDocumentImpl。
    • 我已经在 Buider Factory 中检查过,即在我的 java 代码中。问题在文档中 htmlDoc = MyHTMLDocumentImpl.makeBasicHtmlDoc("My Title");本声明。
    猜你喜欢
    • 1970-01-01
    • 2018-11-30
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2018-05-17
    • 2016-03-29
    • 2022-10-13
    相关资源
    最近更新 更多