[英]How to create xml Document using result of XPath
I'm reading xml document using XPath and I need to create a Document object using the result of XPath evaluation. 我正在使用XPath阅读xml文档,并且需要使用XPath评估结果创建一个Document对象。 Can some one tell me how to do this??
有人可以告诉我该怎么做吗?
Assuming that your xpath returns a single node, you can do something like: 假设您的xpath返回单个节点,则可以执行以下操作:
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.