You should do it as follows:
- you create a new
org.w3c.dom.Document newXmlDoc where you store the nodes in a NodeList , - you create a new root element and add it to
newXmlDoc - then for each node
n in your NodeList you import n into newXmlDoc and then add n as a child of root
Here is the code:
public static void main(String[] args) { String exp = "/configs/markets/market"; String path = "src/a/testConfig.xml"; try { Document xmlDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(path); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(exp); NodeList nodes = (NodeList) xPathExpression. evaluate(xmlDocument, XPathConstants.NODESET); Document newXmlDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); Element root = newXmlDocument.createElement("root"); newXmlDocument.appendChild(root); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Node copyNode = newXmlDocument.importNode(node, true); root.appendChild(copyNode); } printTree(newXmlDocument); } catch (Exception ex) { ex.printStackTrace(); } } public static void printXmlDocument(Document document) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) document.getImplementation(); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); String string = lsSerializer.writeToString(document); System.out.println(string); }
Conclusion:
<?xml version="1.0" encoding="UTF-16"?> <root><market> <name>Real</name> </market><market> <name>play</name> </market></root>
Some notes:
- I changed
exp to /configs/markets/market because I suspect you want to copy market elements, not one markets element - for
printXmlDocument , I used interesting code in the answer
Hope this helps.
If you do not want to create a new root element, you can use your original XPath expression, which returns a NodeList consisting of one node (remember that your XML must have one element root), which can be directly added to the new XML document.
See the following code where I commented out the lines from the code above:
public static void main(String[] args) { //String exp = "/configs/markets/market/"; String exp = "/configs/markets"; String path = "src/a/testConfig.xml"; try { Document xmlDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(path); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(exp); NodeList nodes = (NodeList) xPathExpression. evaluate(xmlDocument,XPathConstants.NODESET); Document newXmlDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument(); //Element root = newXmlDocument.createElement("root"); //newXmlDocument.appendChild(root); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); Node copyNode = newXmlDocument.importNode(node, true); newXmlDocument.appendChild(copyNode); //root.appendChild(copyNode); } printXmlDocument(newXmlDocument); } catch (Exception ex) { ex.printStackTrace(); } }
This will give you the following result:
<?xml version="1.0" encoding="UTF-16"?> <markets> <market> <name>Real</name> </market> <market> <name>play</name> </market> </markets>
source share