Example usage for javax.xml.bind JAXBContext newInstance

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

Introduction

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

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:Main.java

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

        @Override// w  w w  .  java  2  s .c om
        public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
            System.out.println(suggestedFileName);
            return new StreamResult(suggestedFileName);
        }

    });

}

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);
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override//  w ww  .  j  a  v  a 2s  .c  o m
        public boolean handleEvent(ValidationEvent ve) {
            System.out.println(ve.getMessage());
            return true;
        }
    });

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

From source file:JavaToXMLDemo.java

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

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

    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Cath");
    object.setSalary(300);/*w  ww.  java 2s. co m*/

    m.marshal(object, new FileOutputStream("result.xml"));

}

From source file:JavaToXMLDemo.java

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

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

    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Cath");
    object.setSalary(300);/* w w  w .  ja v a 2  s  . c  o  m*/

    m.marshal(object, System.out);

}

From source file:UnmarshallingDemo.java

public static void main(String[] args) {
    try {//from  www . j  a v a  2  s  .  c om
        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();
    }
}

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 .jav a2  s  .  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(Wrapper.class);
    Wrapper wrapper = new Wrapper();
    Hashtable<String, String> hashtable = new Hashtable<String, String>();
    hashtable.put("foo", "A");
    hashtable.put("bar", "B");
    wrapper.setHashtable(hashtable);//  w w w .j av a2  s  .  com
    System.out.println(objectToXml(jc, wrapper));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xmlString = "<message>HELLO!</message> ";
    JAXBContext jc = JAXBContext.newInstance(String.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StreamSource xmlSource = new StreamSource(new StringReader(xmlString));
    JAXBElement<String> je = unmarshaller.unmarshal(xmlSource, String.class);
    System.out.println(je.getValue());
}

From source file:Main.java

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

    Child child = new Child();
    child.setParentProp("parent-value");
    child.setChildProp("child-value");

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

From source file:Main.java

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

    Bar bar = new Bar();
    bar.setAtt1("a");
    bar.setAtt2("b");

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