Java – create XML documents using NodeList
I need to create an XML document object using NodeList Someone can do it for me I've shown you the following code and XML
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.*; import org.w3c.dom.*; public class ReadFile { public static void main(String[] args) { String exp = "/configs/markets"; String path = "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); } catch (Exception ex) { ex.printStackTrace(); } } }
The XML file is shown below
<configs> <markets> <market> <name>Real</name> </market> <market> <name>play</name> </market> </markets> </configs>
Thank you in advance
Solution
You should do this:
>You create a new org w3c. dom. Document newxmldoc, where nodes in NodeList are stored, > you create a new root element and attach it to newxmldoc > then, for each node n in NodeList, import n in newxmldoc, and then attach n as a child node of root
This 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); }
The output is:
<?xml version="1.0" encoding="UTF-16"?> <root><market> <name>Real</name> </market><market> <name>play</name> </market></root>
Some notes:
>I have changed exp to / configs / markets / market because I suspect you want to copy market elements rather than a single market element > for printxmldocument, I used interesting code in this answer
I hope it helps
If you don't want to create a new root element, you can use the original XPath expression, which returns a NodeList composed of a single node (remember, your XML must have only one root element), which you can add directly to your new XML document
See the following code, where I commented on the lines in the above code:
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 output:
<?xml version="1.0" encoding="UTF-16"?> <markets> <market> <name>Real</name> </market> <market> <name>play</name> </market> </markets>