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

Deepak_Mahalingam

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can i remove an element or attribute from a json output while parsing from xml, using java

From Dev

Read values from a complex XML using java

From Dev

How can I save data from xml to sql 2008?

From Dev

Using streams, how can I map the values in a HashMap?

From Dev

how can I access the values of XML nodes using DOM API

From Dev

How can I read a text file from the terminal and save the output to another file in java?

From Dev

How to save xml configuration filled with values from properties file in Apache Commons Configuration can't save

From Dev

How can I use java 8 streams to get values from my hashmap

From Dev

How can I read GSettings values from a filesystem backup?

From Dev

How can i populate a RecyclerView with hashmap values?

From Dev

Can not read values/nodes from xml using c#

From Dev

How can I to read a XML from a URL using T-SQL?

From Dev

How can I read GSettings values from a filesystem backup?

From Dev

How can I read out XML in android(java)

From Dev

How to read tag values from XML from server directly in java?

From Dev

How can i read in attributes from an XML document in C#?

From Dev

Can I use String[] as HashMap values in Java?

From Dev

How can I read keys and values from nestable list?

From Dev

How can I read file.xml from the same package?

From Dev

How can I read a text file from the terminal and save the output to another file in java?

From Dev

how can i read multiple xml values from URL through t-sql

From Dev

How can I improve how I read XML from Socket

From Dev

How can I read all data from text plain using xslt in Java?

From Dev

How can I use java 8 streams to get values from my hashmap

From Dev

How to get values from a Java HashMap in javascript function using Rhino

From Dev

How can I put multiple values to ArrayList using HashMap?

From Dev

how can i save XmL data from a URL into my database

From Dev

How can I read content from web using Java?

From Dev

How can I read this xml?

Related Related

  1. 1

    How can i remove an element or attribute from a json output while parsing from xml, using java

  2. 2

    Read values from a complex XML using java

  3. 3

    How can I save data from xml to sql 2008?

  4. 4

    Using streams, how can I map the values in a HashMap?

  5. 5

    how can I access the values of XML nodes using DOM API

  6. 6

    How can I read a text file from the terminal and save the output to another file in java?

  7. 7

    How to save xml configuration filled with values from properties file in Apache Commons Configuration can't save

  8. 8

    How can I use java 8 streams to get values from my hashmap

  9. 9

    How can I read GSettings values from a filesystem backup?

  10. 10

    How can i populate a RecyclerView with hashmap values?

  11. 11

    Can not read values/nodes from xml using c#

  12. 12

    How can I to read a XML from a URL using T-SQL?

  13. 13

    How can I read GSettings values from a filesystem backup?

  14. 14

    How can I read out XML in android(java)

  15. 15

    How to read tag values from XML from server directly in java?

  16. 16

    How can i read in attributes from an XML document in C#?

  17. 17

    Can I use String[] as HashMap values in Java?

  18. 18

    How can I read keys and values from nestable list?

  19. 19

    How can I read file.xml from the same package?

  20. 20

    How can I read a text file from the terminal and save the output to another file in java?

  21. 21

    how can i read multiple xml values from URL through t-sql

  22. 22

    How can I improve how I read XML from Socket

  23. 23

    How can I read all data from text plain using xslt in Java?

  24. 24

    How can I use java 8 streams to get values from my hashmap

  25. 25

    How to get values from a Java HashMap in javascript function using Rhino

  26. 26

    How can I put multiple values to ArrayList using HashMap?

  27. 27

    how can i save XmL data from a URL into my database

  28. 28

    How can I read content from web using Java?

  29. 29

    How can I read this xml?

HotTag

Archive