Total members 10262 | Gratitudes |It is currently Wed May 23, 2012 8:34 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Code Snippet
 Code subject: Unmarshall From XML using JAXB
PostPosted: Wed Jul 08, 2009 12:23 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

Unmarshall From XML using JAXB :

This is example unmarshall the content of Student xml into the java objects .
Code:


import jaxbclasses
.studentType;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class 
UnmarshallingTest {

    public static 
void main (String [] args) {
        try {
            
JAXBContext jc JAXBContext.newInstance ("jaxbclasses");

            
Unmarshaller u jc.createUnmarshaller ();

           
File f = new File ("student.xml");
           
JAXBElement element = (JAXBElementu.unmarshal (f);

           
studentType student= (studentTypeelement.getValue ();
           
System.out.println ("Student Gender is  : " student.getGender ());
           
System.out.println ("Student Name is : " item.getName ());
           
System.out.println ("Student Age is : " item.getAge ());
       } catch (
JAXBException e) {
           
e.printStackTrace ();
       }
   }
}
 


Here is the schema ( XSD file ) .
Code:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <
xsd:element name="student" type="studentType"/>
    <
xsd:complexType name="studentType">
        <
xsd:sequence>
            <
xsd:element name="gender" type="xsd:string"/>
            <
xsd:element name="name" type="xsd:string"/>
            <
xsd:element name="age" type="xsd:integer"/>
        </
xsd:sequence>
    </
xsd:complexType>
</
xsd:schema

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )



For this message the author msi_333 has received gratitude : manniar
TOP
 Profile Send private message  
Reply with quote  
 Code subject: Re: Unmarshall From XML using JAXB
PostPosted: Wed Jan 26, 2011 12:43 pm 
Offline
Newbie
User avatar

Joined: Wed Jan 26, 2011 12:36 pm
Posts: 1
Has thanked: 0 time
Have thanks: 1 time
I think there is a small typo in your example,

I think the code should read,
Code:
           
System.out.println ("Student Name is : " + student.getName ());
System.out.println ("Student Age is : " + student.getAge ());


Also it might be worth mentioning that you need to generate the java class from the XSD file you've mentioned. I used the utility plug-in for Eclipse called XJC. Then the code in your example will find the java class for StudentType. Might be obvious for experienced programmers but not for people new to Jaxb.

Good example though, thanks.



For this message the author robbie70 has received gratitude : msi_333
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 how to use enum in jaxb unmarshalling
 problem with binding a choice model with jaxb
 adding an xmlns attribute xith jaxb
 insert a namespace in a tag with jaxb
 update an xml file with jaxb and accessing to its elements
 update an xml file with jaxb
 XML using JAXB API in java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team