Total members 11890 |It is currently Fri Apr 19, 2024 7:16 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> 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


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

For this message the author DrRakha 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 : DrRakha
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     -  
 update an xml file with jaxb and accessing to its elements     -  
 adding an xmlns attribute xith jaxb     -  
 problem with binding a choice model with jaxb     -  



Topic Tags

Java XML, Java JAXB
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com