XML parsing using JAX-B

Download XmlParsing in Java from here or here.


Steps to generate JAXB classes:


Given XML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<school>
    <student id="1">
        <firstname>ankush</firstname>
        <lastname>thakur</lastname>
        <email>beingjavaguy.gmail.com</email>
        <phone>7678767656</phone>
    </student>
    <student id="2">
        <firstname>aarti</firstname>
        <lastname>gupta</lastname>
        <email>aartigupta.gmail.com</email>
        <phone>9876546545</phone>
    </student>
</school>



1] Convert xml file to XSD file for schema validation by visiting:

2] After pasting xml click generate xsd.


Click to Zoom

 
3] Paste xsd file in Eclipse

4] Now create a package in src folder where jaxb classes will be generated.

5] Right click your xsd file in eclipse-> Generate-> Jaxb classes....

Click to zoom














6] Pop-up window will appear. Choose your project->click next.
    In package section choose the package where you want the generated jaxb classes.(Read point No.4)

 
Click to Zoom





















7] Then click next->next->finish

8]  A pop-up will appear. Click yes

9] JaxB classes are generated. Check your src-->classes folder created in step four. You can remove unwanted comments from generated classes. ObjectFactory class is not of much use. Just ignore it.

Click to Zoom

10] Override toString method in pojo class ie. School.java in this example.
To override goto School.java class. Right click-->goto Source-->click Generate toString()....--> Check all the fields--> Click ok.

Click to zoom

Click to Zoom


11] Finally write the code to read the given XML.
FileName:  JaxB_ReadXml.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package classes;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import classes.School.Student;

public class JaxB_ReadXml {
    public static void main(String[] args) {

     try {

        File file = new File("xmlToRead/student.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(School.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        School School = (School) jaxbUnmarshaller.unmarshal(file);
       
        List<Student> output_in_list = School.getStudent(); //  this line reads the list(student) from School.java
        System.out.println(output_in_list);
       
       
        System.out.println("##########  FOR EACH LOOP   ################");   
       
        for (Student c : output_in_list) {
             System.out.println("\n ID:"+c.id+ "\n Firstname:"+c.firstname+"\n Lastname:"+c.lastname+"\n Email:"+c.email+"\n Phone:"+c.phone);
            }

       
       
      } catch (JAXBException e) {
        e.printStackTrace();
      }

    }
}



No comments:

Post a Comment