Example usage for javax.xml.bind JAXBContext createUnmarshaller

List of usage examples for javax.xml.bind JAXBContext createUnmarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createUnmarshaller.

Prototype

public abstract Unmarshaller createUnmarshaller() throws JAXBException;

Source Link

Document

Create an Unmarshaller object that can be used to convert XML data into a java content tree.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Root.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("input.xml");
    Root root = (Root) unmarshaller.unmarshal(xml);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(root, System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Compound.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Compound compound = (Compound) unmarshaller.unmarshal(new File("input.xml"));
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(compound, System.out);
}

From source file:PersonUnmarshaller.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("person");
    Unmarshaller unmarshaller = context.createUnmarshaller();

    Person person = (Person) unmarshaller.unmarshal(new File("person.xml"));
    System.out.println(person.getFirstName());
}

From source file:MisspelledPersonUnmarshaller.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("person");
    Unmarshaller unmarshaller = context.createUnmarshaller();

    JAXBElement<Person> element = unmarshaller.unmarshal(new StreamSource("p.xml"), Person.class);
    Person person = element.getValue();/* w  ww  .j a  v  a2 s .  c  o m*/
    System.out.println(person.getFirstName());

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Order o = new Order();
    o.setCustId(123);// w  w  w  . jav a 2  s. c o  m
    o.setDescription("New order");
    o.setOrderDate(new Date());

    List<Item> items = new ArrayList<Item>();

    Item i = new Item();
    i.setName("PC");
    i.setQty(10);
    items.add(i);

    i = new Item();
    i.setName("Box");
    i.setQty(4);

    items.add(i);

    o.setItems(items);
    // Write it
    JAXBContext ctx = JAXBContext.newInstance(Order.class);

    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    m.marshal(o, sw);
    sw.close();
    System.out.println(sw.toString());

    // Read it back
    JAXBContext readCtx = JAXBContext.newInstance(Order.class);
    Unmarshaller um = readCtx.createUnmarshaller();

    Order newOrder = (Order) um.unmarshal(new StringReader(sw.toString()));
    System.out.println(newOrder);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Address.class);

    StringReader xml = new StringReader("<Address><Name>Test</Name></Address>");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Address address = (Address) unmarshaller.unmarshal(xml);

    System.out.println(address.getPostalAddress().getState());

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(address, System.out);
}

From source file:webservices.simple_jaxb.UnmarshalRead.java

public static void main(String[] args) {
    try {/*from  w ww  . ja  v  a 2  s  .  c om*/
        // create a JAXBContext capable of handling classes generated into
        // the primer.po package
        JAXBContext jc = JAXBContext.newInstance("primer.po");

        // create an Unmarshaller
        Unmarshaller u = jc.createUnmarshaller();

        // unmarshal a po instance document into a tree of Java content
        // objects composed of classes from the primer.po package.
        JAXBElement<?> poElement = (JAXBElement<?>) u.unmarshal(new File("po.xml"));
        PurchaseOrderType po = (PurchaseOrderType) poElement.getValue();

        // examine some of the content in the PurchaseOrder
        System.out.println("Ship the following items to: ");

        // display the shipping address
        USAddress address = po.getShipTo();
        displayAddress(address);

        // display the items
        Items items = po.getItems();
        displayItems(items);

    } catch (JAXBException je) {
        je.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class, ObjectFactory.class);

    StringReader xml = new StringReader("<foo><C>Hello World</C></foo>");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    JAXBElement<?> aOrBOrCOrD = foo.aOrBOrCOrD;
    System.out.println(aOrBOrCOrD.getName().getLocalPart());
    System.out.println(aOrBOrCOrD.getDeclaredType());
    System.out.println(aOrBOrCOrD.getValue());
}

From source file:org.jasig.portlet.maps.tools.MapDataTransformer.java

/**
 * @param args/*from w  w  w  .j av a 2  s . co  m*/
 */
public static void main(String[] args) {

    // translate the KML file to the map portlet's native data format
    File kml = new File("map-data.xml");
    File xslt = new File("google-earth.xsl");

    try {
        TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer(new StreamSource(xslt));
        trans.transform(new StreamSource(kml), new StreamResult(System.out));
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    // deserialize the map data from XML
    MapData data = null;
    try {
        JAXBContext jc = JAXBContext.newInstance(MapData.class);
        Unmarshaller u = jc.createUnmarshaller();
        data = (MapData) u.unmarshal(new FileInputStream(new File("map-data-transformed.xml")));
    } catch (JAXBException e1) {
        e1.printStackTrace();
        return;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }

    // ensure each location has a unique, non-null abbreviation
    setAbbreviations(data);

    // update each location with an address
    //        setAddresses(data);

    // sort locations by name
    Collections.sort(data.getLocations(), new ByNameLocationComparator());

    // serialize new map data out to a file into JSON format
    try {
        mapper.defaultPrettyPrintingWriter().writeValue(new File("map.json"), data);
    } catch (JsonGenerationException e) {
        System.out.println("Error generating JSON data for map");
        e.printStackTrace();
    } catch (JsonMappingException e) {
        System.out.println("Error generating JSON data for map");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Error writing JSON data to map file");
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Customer.class);

    XMLInputFactory xif = XMLInputFactory.newFactory();
    XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new File("input.xml")));

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    unmarshaller.setListener(new LocationListener(xsr));
    Customer customer = (Customer) unmarshaller.unmarshal(xsr);
}