Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

How to retreive data from XML File in Java.

4.20/5 (4 votes)
14 May 2014CPOL2 min read 16.3K   126  
This tip shows you how to easily retreive soma data from xml file

Download WorkshopXML.zip

Introduction

There are many ways to store data. You can use a Text File or Database or even XML File. It is very important to use xml to store and retreive from because it is very simple , easy and designed to be self-descriptive.

Background

Extensible Markub Language (XML) is a markup language that defines some set of rules for encoding in a simple and readable format. It was disigned to carry data and not to display data.So, As we said XML is created to store, structure and transport data

Using the code

the first step we need to do is to add an External Jar Library called "Jdom-2.0.5.jar" and add the xml file that you want to add.In our case our file called "TauxChomage.xml". click right on the project and choose properties as shown :

Image 1

the xml file contains the rate of jobless in different countries. For example in Canada the rate jobless in 2005 is 7.0 for men and 9.5 for women as shown below:

Image 2

The question here : how we can retreive these data from this xml file to manipulate it ?

we want to display just the average rate in a specific datefor each country.we want to create a method in which we will make our process starting by reading our file in an element called "chommage"

C++
public static void readXmlFile() throws JDOMException, IOException
    {
             
        Element Chommage = ((Document)(new SAXBuilder()).build(new File ("TauxChomage.xml"))).getRootElement(); 

In order to read all countries we need to use this piece of code:

C++
List listpays = Chommage.getChildren("pays");

After that we need to reach all of the racine to extract the name and the rate value and to store the Countries rate in three memory cases that represents a specific date

C++
Iterator i = listpays.iterator();
       while(i.hasNext())
       {

          Element courant = (Element)i.next();
          Pays.add(courant.getAttributeValue("nom"));
          System.out.print(courant.getAttributeValue("nom"));


          List listTaux = courant.getChildren("taux");

         Iterator j = listTaux.iterator();

        float t1=0,t2=0,t3=0;
         while(j.hasNext())
         {


           //  somme=(int) listTaux.get(k);
             Element tauxElem= (Element)j.next();
               switch (tauxElem.getAttribute("annee").getValue())
               {
               case "2005":
                    t1+=Float.parseFloat(tauxElem.getText());

                   break;
               case "2006":
                    t2+=Float.parseFloat(tauxElem.getText());
                   break;
               case "2007":
                    t3+=Float.parseFloat(tauxElem.getText());
                   break;

               }

         }

Now, we can manipulate the result and display it easily:

C++
somme1+=t1;somme2+=t2;somme3+=t3;
          Taux2005.add(""+ (t1/2));
        
          System.out.print(" | Annee 2006 : "+t2/2);
          Taux2006.add(""+ (t2/2));
          
          System.out.println(" | Annee 2007 : "+t3/2);
          Taux2007.add(""+ (t3/2));
          
        }
      //taux globale
        System.out.println("Le taux de chommage globale de L'annee 2005 "+somme1/24);
        System.out.println("Le taux de chommage globale de L'annee 2006 "+somme2/24);
        System.out.println("Le taux de chommage globale de L'annee 2007 "+somme3/24);
        
        }

Now, when we compile the program, we see this result:

Image 3

Well done! Working with Xml is very easy as we saw together in this tip. Download the code source and you can understand more with it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)