Note that there are some explanatory texts on larger screens.

plurals
  1. POXML Structure creations issue
    primarykey
    data
    text
    <p>I data in the below format (List of HashMap's)</p> <pre><code>{TeamName=India, Name=Sachin, Score=170} {TeamName=India, Name=Sehwag, Score=120} {TeamName=Sri-Lanka, Name=Sangakara, Score=20} {TeamName=Sri-Lanka, Name=Murali, Score=20} {TeamName=Sri-Lanka, Name=Jayasurya, Score=70} </code></pre> <p>Below is the desired output</p> <pre><code>&lt;node id="1" label="India" &gt; &lt;node id="1.1" label="Sachin" Score="170" /&gt; &lt;node id="1.2" label="Sehwag" Score="120" /&gt; &lt;/node&gt; &lt;node id="2" label="Sri-Lanka"&gt; &lt;node id="2.1" label="Sangakara" Score="20" /&gt; &lt;node id="2.2" label="Murali" Score="20" /&gt; &lt;node id="2.3" label="Jayasurya" Score="70" /&gt; &lt;/node&gt; &lt;node id="3" label="World-XI"&gt; &lt;node id="2.2" label="Murali" Score="20" /&gt; &lt;node id="1.1" label="Sachin" Score="170" /&gt; &lt;/node&gt; </code></pre> <p>Now, I have been able to generate the above structure, but have a problem, the id in "node id=3" needs to be repeated not re-created as 3.1/3.2.</p> <p>Another is that, it does not have to be that node 3 is the last and I could just iterate only for that, but there could be many other nodes.</p> <p>Below is the current piece of code the above, any suggestions?</p> <pre><code>Map hm = new HashMap(); Element em = null; try { int serverId = 0; int clientId = 0; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement("CricketDetails"); document.appendChild(rootElement); for (int i=0; i &lt; l.size(); i++) { hm = (HashMap) l.get(i); sortListIP.add(hm.get("TeamName")); } Collections.sort(sortListIP); HashSet h = new HashSet(sortListIP); sortListIP.clear(); sortListIP.addAll(h); for (int i=0; i &lt; sortListIP.size(); i++) { ++serverId; clientId = 0; em = document.createElement("node"); em.setAttribute("id", ""+serverId); em.setAttribute("TeamName", ""+sortListIP.get(i)); for (int j=0; j &lt; l.size();j++) { hm = (HashMap) l.get(j); if (sortListIP.get(i).equals(hm.get("TeamName"))) { Element em_child = document.createElement("node"); ++clientId; em_child.setAttribute("id", serverId+"."+clientId); em_child.setAttribute("label", (String) hm.get("Name")); em.appendChild(em_child); rootElement.appendChild(em); } } } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult("cricketDetails.xml"); transformer.transform(source, result); </code></pre> <p>P.S. The format defined may not be correct, but the expected output is required like this only!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload