I'm reading xml document using XPath and I need to create a Document object using the result of XPath evaluation. Can some one tell me how to do this??
Assuming that your xpath returns a single node, you can do something like:
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc.appendChild(doc.importNode(xpathResult, true));
If it returns a node set, you will have to create a root element yourself.
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
doc.appendChild(doc.createElement("root"));
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
doc.getDocumentElement().appendChild(doc.importNode(node, true));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.