gpt4 book ai didi

java - 为什么我在这里得到 "org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR"?

转载 作者:行者123 更新时间:2023-11-30 03:19:06 27 4
gpt4 key购买 nike

完整的异常堆栈:

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);
}
}

测试.html

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

最佳答案

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

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

文档中有两个根,因此会出现异常。

关于java - 为什么我在这里得到 "org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31778140/

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