Unmarshall From XML using JAXB : JAXB « JDK 6 « Java

Unmarshall From XML using JAXB

import generated.Item;
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 UnmarshallingDemo {

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

            Unmarshaller u = jc.createUnmarshaller ();

           File f = new File ("item.xml");
           JAXBElement element = (JAXBElement) u.unmarshal (f);

           Item item = (Item) element.getValue ();
           System.out.println (item.getCode ());
           System.out.println (item.getName ());
           System.out.println (item.getPrice ());
       } catch (JAXBException e) {
           e.printStackTrace ();

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="item" type="Item"/>
    <xsd:complexType name="Item">
            <xsd:element name="code" type="xsd:string"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="price" type="xsd:double"/>


JAXBUnmarshallFromXML.zip( 5 k)

Related examples in the same category

1.simple jaxb
2.Marshal Java object to xml and output to console
3.Marshal Java object to a file
4.JAXB Demo
5.From XML Schema to Java Generic List
6.JAXB XML Schema Enum and Java Enum
7.Set Target Name Space To Be Java Package Name
8.Generate Java Source From XML Schema (XSD)