Unmarshaller: unmarshal(File f) : Unmarshaller « javax.xml.bind « Java by API






Unmarshaller: unmarshal(File f)

  



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:sequence>
            <xsd:element name="code" type="xsd:string"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="price" type="xsd:double"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

   
    
  








Related examples in the same category