【问题标题】:Why I get "org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR" Here?为什么我在这里得到“org.w3c.dom.DOMException:HIERARCHY_REQUEST_ERR”?
【发布时间】:2015-10-25 00:28:42
【问题描述】:

完整的异常堆栈:

Exception in thread "main" org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted. 
    at org.apache.xerces.dom.CoreDocumentImpl.insertBefore(Unknown Source)
    at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)
    at com.enniu.crawler.core.saxon.main(saxon.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

我的代码:

public class saxon {

    public static void main(String args[]) throws IOException, SAXException, ParserConfigurationException, XPathFactoryConfigurationException, XPathExpressionException {

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = null;
        builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("test.html");
        Document newDoc = builder.newDocument();
        XPathFactory xpf = XPathFactoryImpl.newInstance(XPathConstants.DOM_OBJECT_MODEL);
        XPath xPath = xpf.newXPath();
        XPathExpression compile = xPath.compile("//div[not (contains(class, 'sss'))]");
        Object result = compile.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for(int i = 0; i < nodes.getLength(); i++) {
            Node copyNode = newDoc.importNode(nodes.item(i), true);
            newDoc.appendChild(copyNode);// line 39
        }
        printXmlDocument(newDoc);
    }

    public static void printXmlDocument(Document document) {
        DOMImplementationLS domImplementationLS =
                (DOMImplementationLS) document.getImplementation();
        LSSerializer lsSerializer =
                domImplementationLS.createLSSerializer();
        String string = lsSerializer.writeToString(document);
        System.out.println(string);
    }
}

test.html

<table>
    <div>aa</div>
    <div class="sss">ss</div>
    <div>dd</div>
</table>

【问题讨论】:

    标签: java html xpath


    【解决方案1】:

    因为一个有效的 http 文档不能有两个根。我的代码尝试生成这样的文档:

    <div>aa</div>
    <div>dd</div>
    

    文档中有两个根,所以获取异常。

    【讨论】:

      猜你喜欢
      • 2020-07-31
      • 2021-11-08
      • 2016-11-22
      • 1970-01-01
      • 2016-09-26
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多