Total members 11638 |It is currently Thu May 25, 2017 4:21 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Unmarshall From XML using JAXB :

This is example unmarshall the content of Student xml into the java objects .
java 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 = (JAXBElement) u.unmarshal (f);

           studentType student= (studentType) element.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 ) .
xml 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> 






Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time

For this message the author msi_333 has received gratitude : manniar

I think there is a small typo in your example,

I think the code should read,
java 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.


Author:
Newbie
User avatar Posts: 1
Have thanks: 1 time

For this message the author robbie70 has received gratitude : msi_333
Post new topic Reply to topic  [ 2 posts ] 

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



Topic Tags

Java XML, Java JAXB






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