tags:

views:

46

answers:

1

I data in the below format (List of HashMap's)

{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}

Below is the desired output

<node id="1" label="India" >
        <node id="1.1" label="Sachin" Score="170" />
        <node id="1.2" label="Sehwag" Score="120" />
</node>
<node id="2" label="Sri-Lanka">
      <node id="2.1" label="Sangakara" Score="20" />
      <node id="2.2" label="Murali" Score="20" />
      <node id="2.3" label="Jayasurya" Score="70" />
</node>
<node id="3" label="World-XI">
      <node id="2.2" label="Murali" Score="20" />
      <node id="1.1" label="Sachin" Score="170" />
</node>

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.

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.

Below is the current piece of code the above, any suggestions?

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

P.S. The format defined may not be correct, but the expected output is required like this only!

+1  A: 

If you want to modify the attribute id for a node that gets generated with your code modify the line that contains em_child.setAttribute("id", serverId+"."+clientId); appropriately. The parent nodes id gets generated similarly.

I'm not sure if your xml is just incomplete or not but in XML you are required to have root or document entity.

"A [XML] document begins in a "root" or document entity." - http://www.w3.org/TR/REC-xml/#sec-documents

Derek Litz
The XML code work perfectly fine, I've tested it and I ofcourse I do have a root element. The only problem I'm having is the iteration and I need help on that only.
Panther24