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}

I have to generate the following XML structure:

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

How can I do this?

2
  • 3
    I'm going to assume that you know how to iterate a Map, and write basic Java loops, so will point you to an article I wrote on XML output: kdgregory.com/index.php?page=xml.builder Commented Dec 22, 2009 at 12:52
  • @kdgregory Your link is excellent, worth reading and possibly discussing. I suggest you make a full answer out of it! I need to vote for it, and remember and bookmark it! :-) Commented Dec 22, 2009 at 13:43

1 Answer 1

2
  1. Create a nested structure of HashMaps to collect the teams. The outer map has the name of the team as key and another map as value. The inner map maps players to scores.

  2. Iterate over this structure and emit the XML with the XML writer of your choice. Try JDom or StAX.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.