Example usage for javax.xml.bind Unmarshaller unmarshal

List of usage examples for javax.xml.bind Unmarshaller unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller unmarshal.

Prototype

public Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;

Source Link

Document

Unmarshal XML data from the specified pull parser and return the resulting content tree.

Usage

From source file:Main.java

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

    XMLInputFactory xif = XMLInputFactory.newInstance();
    FileInputStream fis = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(fis);
    xsr.nextTag();//from w w  w.j  av  a 2s . c o m
    String noNamespaceSchemaLocation = xsr.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
            "noNamespaceSchemaLocation");
    System.out.println(noNamespaceSchemaLocation);

    Unmarshaller um = context.createUnmarshaller();
    User response = (User) um.unmarshal(xsr);
}

From source file:Main.java

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

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Root root = (Root) unmarshaller.unmarshal(new File("input.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 {
    String xml1 = "<abc><name>hello</name></abc>";
    String xml2 = "<xyz><name>hello</name></xyz>";
    Unmarshaller unmarshaller = JAXBContext.newInstance(Foo.class).createUnmarshaller();
    Object o1 = unmarshaller.unmarshal(new StringReader(xml1));
    Object o2 = unmarshaller.unmarshal(new StringReader(xml2));
    System.out.println(o1);//from  ww w  . j  av  a 2s .c  o  m
    System.out.println(o2);
}

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: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(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:Main.java

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

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader xml = new StringReader("<foo><bar>toast</bar></foo>");
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    System.out.println(foo.isBar());
}

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:Main.java

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

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("input.xml");
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    System.out.println(foo.getBar());

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(foo, 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());
}