Deepak_Mahalingam
Deepak_Mahalingam

Reputation: 454

How can I read values from XML and save it in Hashmap using Java

I'm new to XML parsing, I trying to retrieve data following XML n save it in hashmap. I want Description, id and name from each Fields to be saved in Hashmap.

 <Entities TotalResults="13689">
         <Entity Type="test">
              <Fields>
                   <Field Name="description">
                       <Value>I want to print THIS</Value>
                   </Field>
                   <Field Name="id"><Value>1357</Value></Field>
                   <Field Name="vc-comments"><Value></Value></Field>
                   <Field Name="name">
                       <Value>locked manager - lock state</Value>
                   </Field>
                   <Field Name="has-linkage"><Value>N</Value></Field>
               </Fields>
         </Entity>
         <Entity Type="test">
              <Fields>
                   <Field Name="description"><Value>Print this</Value></Field>
                   <Field Name="user-06"><Value></Value></Field>
                   <Field Name="id"><Value>1358</Value></Field>
                   <Field Name="name">
                       <Value>locked manager - stealing a key </Value>
                   </Field>
                   <Field Name="vc-status"><Value></Value></Field>
              </Fields>
         </Entity>
     </Entities>

Upvotes: 1

Views: 3120

Answers (2)

KishanCS
KishanCS

Reputation: 1377

You should use hash map when you dont know the fields

The rite way for you problem is to build a pojo class like this

public class MyPojo
{
    private Entities Entities;

    public Entities getEntities ()
    {
        return Entities;
    }

    public void setEntities (Entities Entities)
    {
        this.Entities = Entities;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [Entities = "+Entities+"]";
    }
}


public class Entities
{
    private String TotalResults;

    private Entity[] Entity;//you can use List<> insted 

    public String getTotalResults ()
    {
        return TotalResults;
    }

    public void setTotalResults (String TotalResults)
    {
        this.TotalResults = TotalResults;
    }

    public Entity[] getEntity ()
    {
        return Entity;
    }

    public void setEntity (Entity[] Entity)
    {
        this.Entity = Entity;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [TotalResults = "+TotalResults+", Entity = "+Entity+"]";
    }
}

I have made 2 pojos for your better understanding

you can create the rest as related to xml. Later you can just use

            File file = new File("My.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(MyPojo.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            MyPojo myPojo = (MyPojo) jaxbUnmarshaller.unmarshal(file);
            System.out.println(myPojo);//get your value with getter setter.

//Description, id and name can be retrieved.

In general, you use a Collection (List, Map, Set) to store objects with similar characteristics, that's why generics exist.

Upvotes: 3

Jigar Shah
Jigar Shah

Reputation: 470

You can visit this tutorial for parsing the XML file in java XML Parsing In Java

Then you just need to store in a hashmap.

Upvotes: 1

Related Questions