Click here to Skip to main content
15,392,858 members
Articles / Programming Languages / Java
Posted 14 May 2014

Tagged as


9 bookmarked

How to retreive data from XML File in Java.

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
14 May 2014CPOL2 min read
This tip shows you how to easily retreive soma data from xml file



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.


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"

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:

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

Iterator i = listpays.iterator();

          Element courant = (Element);

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

         Iterator j = listTaux.iterator();

        float t1=0,t2=0,t3=0;

           //  somme=(int) listTaux.get(k);
             Element tauxElem= (Element);
               switch (tauxElem.getAttribute("annee").getValue())
               case "2005":

               case "2006":
               case "2007":



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

          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.


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


About the Author

Hadrich Mohamed
Tunisia Tunisia
Microsoft Certified Professional, Big Data and Cloud Architect.
Mail :

Comments and Discussions

QuestionWhere to download the "Jdom-2.0.5.jar" file? Pin
ray_dyo16-Dec-14 20:00
Memberray_dyo16-Dec-14 20:00 
AnswerRe: Where to download the "Jdom-2.0.5.jar" file? Pin
Hadrich Mohamed17-Dec-14 3:21
professionalHadrich Mohamed17-Dec-14 3:21 
Question[My vote of 1] What is XML and JDOM? Pin
adatapost21-May-14 19:35
Memberadatapost21-May-14 19:35 
SuggestionIt is better to use DOM parser when dealing with small xml files Pin
xfzheng16-May-14 15:30
Memberxfzheng16-May-14 15:30 
Much flexible and reliable with less coding .
GeneralRe: It is better to use DOM parser when dealing with small xml files Pin
Hadrich Mohamed17-May-14 0:58
professionalHadrich Mohamed17-May-14 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.