Creating an XML document using nodeList

I need to create an XML document object using a NodeList. Can someone please help me do this. This is my Java code:

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(); } } } 

I want to have an XML file like this:

 <configs> <markets> <market> <name>Real</name> </market> <market> <name>play</name> </market> </markets> </configs> 

Thanks in advance.

+6
source share
2 answers

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> 
+13
source

you can try adoptNode() Document method.

You may need to NodeList over your NodeList . You can access a single Nodes using nodeList.item(i) .

If you want to wrap the search results in Element , you can use createElement () from Document and appendChild() on the newly created Element

0
source

Source: https://habr.com/ru/post/886680/


All Articles