2
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);
}
附加一個節點,則拋出在線程「主」 org.w3c.dom.DOMException中異常:WRONG_DOCUMENT_ERR:一個節點在不同的文檔比創建它的一個使用。 at 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 )錯誤文件錯誤而在XML
非常感謝。它節省了我的時間。 – kannanrbk