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 Dev

Read values from a complex XML using java

From Dev

How can I read content from web using Java?

From Dev

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

From Dev

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

From Dev

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

From Dev

How can I put multiple values to ArrayList using HashMap?

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

Can not read values/nodes from xml using c#

From Dev

How can I improve how I read XML from Socket

From Dev

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

From Dev

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

From Dev

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

From Dev

How can I save data from xml to sql 2008?

From Dev

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

From Dev

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

From Dev

How can I read this xml?

From Dev

How can i populate a RecyclerView with hashmap values?

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 read in attributes from an XML document in C#?

From Dev

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

From Dev

How can I read out XML in android(java)

From Dev

How can I read GSettings values from a filesystem backup?

From Dev

How can I read GSettings values from a filesystem backup?

From Dev

How can I read keys and values from nestable list?

From Java

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

From Dev

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

From Dev

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

Related Related

  1. 1

    Read values from a complex XML using java

  2. 2

    How can I read content from web using Java?

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

    How can I put multiple values to ArrayList using HashMap?

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

    Can not read values/nodes from xml using c#

  11. 11

    How can I improve how I read XML from Socket

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    How can I save data from xml to sql 2008?

  16. 16

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

  17. 17

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

  18. 18

    How can I read this xml?

  19. 19

    How can i populate a RecyclerView with hashmap values?

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

    How can I read out XML in android(java)

  24. 24

    How can I read GSettings values from a filesystem backup?

  25. 25

    How can I read GSettings values from a filesystem backup?

  26. 26

    How can I read keys and values from nestable list?

  27. 27

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

  28. 28

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

  29. 29

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

HotTag

Archive