为什么我会得到“org.w3c.dom.DOMException:HIERARCHY_REQUEST_ERR”这里? [英] Why I get "org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR" Here?

查看:314
本文介绍了为什么我会得到“org.w3c.dom.DOMException:HIERARCHY_REQUEST_ERR”这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全异常堆栈:

 线程main中的异常org.w3c.dom.DOMException:HIERARCHY_REQUEST_ERR:在不允许的情况下插入节点。 
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)$ b $ 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节点=(NodeList)结果;
for(int i = 0; i< nodes.getLength(); i ++){
Node copyNode = newDoc.importNode(nodes.item(i),true);
newDoc.appendChild(copyNode); //第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);


$ / code $ / pre>

test.html

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


解决方案

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

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

文档中有两个根,因此得到异常。

Full exception stack:

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)

My Code:

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>

解决方案

Because a valid http document can't have two roots. My code attempt to generate document like this:

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

There are two roots in the document so get the exception.

这篇关于为什么我会得到“org.w3c.dom.DOMException:HIERARCHY_REQUEST_ERR”这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆