public static Node createNodeFromXMLString(String xml) throws SAXException,
IOException {
return builder.parse(new ByteArrayInputStream(xml.getBytes()))
.getDocumentElement();
}
public static void main(String args[]){
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("server.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
Node element = (Node)xpath.evaluate("/Server/Service/Connector[2]",document,XPathConstants.NODE);
String newNode = nodeToString(element).replace("port=\"8443\"", "port=\"8453\"");
Node parent = element.getParentNode();
Node node = createNodeFromXMLString(newNode);
parent.removeChild(element);
document.importNode(node,true);
parent.appendChild(node);
}
它在线程“main”org.w3c.dom.DOMException 中引发异常:WRONG_DOCUMENT_ERR:节点在与创建它的文档不同的文档中使用。在 org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source) at org.apache.xerces.dom.ParentNode.insertBefore(Unknown Source) at org.apache.xerces.dom.NodeImpl.appendChild(Unknown Source)