Creating an XML tree in java and converting it to a json object

I tried to create a tree also able to convert to json. But only for one xpath. When I tried to implement multiple xpath, I could not get the desired result. Here I share my implementation

private static Document addElemtbypath(List<String> pathList1, List<String> elementList) { Document doc = null; try { DocumentBuilderFactory docfactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docfactory.newDocumentBuilder(); doc = docBuilder.newDocument(); Element rootElement = doc.createElement("Tree"); doc.appendChild(rootElement); for (int i = 0; i < pathList1.size(); i++) { String pathList = pathList1.get(i); String element = elementList.get(i); List<String> xpath = new LinkedList<>(); String[] parts = pathList.split("/"); for (int j = 0; j < parts.length; j++) { xpath.add(parts[j]); } System.out.println(xpath); Node node = doc.getDocumentElement(); Document dom = node.getOwnerDocument(); System.out.println(node); for (int k = 0; k < xpath.size(); k++) { XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xPath.evaluate(xpath.get(k), doc.getDocumentElement(), XPathConstants.NODESET); if (nodes.getLength() != 0) { //node = (Node) nodes; // getting class cast exception continue; } else { node = node .appendChild(dom.createElement(xpath.get(k))); } } node.appendChild(dom.createTextNode(element)); // System.out.println(nodes); System.out.println(dom); System.out.println(node); } } catch (Exception e) { e.printStackTrace(); } return doc; } 

and the result is

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Tree> <Product> <Organization> <RegisteredDetail> <something>product1</something> </RegisteredDetail> </Organization> </Product> <Organization> <RegisteredDetail>product2</RegisteredDetail> </Organization> </Tree> 

As you can see the nodes are repeating, I don’t know where I was wrong. My desired result should look like

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Tree> <Product> <Organization> <RegisteredDetail> product2 <something>product1</something> </RegisteredDetail> </Organization> </Product> </Tree> 

One way or another, I am mistaken in repeating the second time.

0
java json xml
Oct 15 '14 at 18:17
source share

No one has answered this question yet.

See similar questions:

51
Java: how to find an element via xpath string on org.w3c.dom.document
0
Creating a dynamic xml tree based on xpath value

or similar:

3799
How do I read / convert an InputStream to a string in Java?
3044
Creating a memory leak using Java
2853
How to convert String to int in Java?
1567
Convert form data to javascript object using jQuery
1315
How to create a file and write it in Java?
1186
Convert JS object to JSON string
1158
Safely turning a JSON string into an object
1153
How to force ASP.NET Web API to return JSON instead of XML using Chrome?
933
How to parse JSON in Java
872
Deserialize JSON to a dynamic C # object?



All Articles