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 {
    Main customer = new Main();
    customer.setId(100);//from www.  j av  a2 s  .c om
    customer.setName("java2s.com");
    customer.setAge(29);

    File file = new File("file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Main.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(customer, file);
    jaxbMarshaller.marshal(customer, System.out);

}

From source file:MyAbstractClass.java

public static void main(String[] args) throws JAXBException {

        MyClass my = new MyClass();
        my.setDataType("data type");
        my.setMatch("STRING MATCH");
        my.setValue("value");

        JAXBContext context = JAXBContext.newInstance(MyClass.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(my, System.out);
    }//  w  w w  .  jav a 2 s  .c o  m

From source file:Main.java

public static void main(final String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Main.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Main event = new Main();
    event.setDate(new Date());
    event.setDescription("im rick james");

    marshaller.marshal(event, System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Main example = new Main();
    example.data.put("France", "Paris");
    example.data.put("Japan", "Tokyo");

    JAXBContext context = JAXBContext.newInstance(Main.class);
    Marshaller marshaller = context.createMarshaller();
    DOMResult result = new DOMResult();
    marshaller.marshal(example, result);

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    Document document = (Document) result.getNode();
    XPathExpression expression = xpath.compile("//map/entry");
    NodeList nodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET);

    expression = xpath.compile("//map");
    Node oldMap = (Node) expression.evaluate(document, XPathConstants.NODE);
    Element newMap = document.createElement("map");

    for (int index = 0; index < nodes.getLength(); index++) {
        Element element = (Element) nodes.item(index);
        newMap.setAttribute(element.getAttribute("key"), element.getAttribute("value"));
    }//ww  w.  j a  v a 2  s. co m

    expression = xpath.compile("//map/..");
    Node parent = (Node) expression.evaluate(document, XPathConstants.NODE);
    parent.replaceChild(newMap, oldMap);

    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document),
            new StreamResult(System.out));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Customer customer = new Customer();
    customer.setName("abcabcabcabcabcabcabc");
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());
    customer.getPhoneNumbers().add(new PhoneNumber());

    JAXBContext jc = JAXBContext.newInstance(Customer.class);
    JAXBSource source = new JAXBSource(jc, customer);

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("customer.xsd"));

    Validator validator = schema.newValidator();
    validator.setErrorHandler(new MyErrorHandler());
    validator.validate(source);// ww w  .  ja  va 2s . co  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Company company = new Company();

    Employee employee1 = new Employee();
    employee1.setId("1");
    employee1.setName("Jane Doe");
    company.getEmployees().add(employee1);

    Employee employee2 = new Employee();
    employee2.setId("2");
    employee2.setName("John Smith");
    employee2.setManager(employee1);/*from  w w w  .ja  v  a 2 s. co m*/
    employee1.getReports().add(employee2);
    company.getEmployees().add(employee2);

    Employee employee3 = new Employee();
    employee3.setId("3");
    employee3.setName("Anne Jones");
    employee3.setManager(employee1);
    employee1.getReports().add(employee3);
    company.getEmployees().add(employee3);

    JAXBContext jc = JAXBContext.newInstance(Company.class);

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

From source file:JAXBTest.java

public static void main(String[] args) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
    Root root = new Root();

    Person fred = new Person();
    fred.setName("Fred");
    root.setFriend(Arrays.asList(fred));

    Thing xbox = new Thing();
    xbox.setDescription("Xbox 360");
    root.setStuff(Arrays.asList(xbox));

    Marshaller marshaller = jaxbContext.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 {
    List<Element> elementList = new ArrayList<Element>();
    List<Item> itemList = new ArrayList<Item>();
    Element element1 = new Element();
    Element element2 = new Element();
    Item item1 = new Item();
    Item item2 = new Item();
    Elements elements = new Elements();

    item1.setId(1);//from w w w  . j  a v  a 2 s  . c o m
    item1.setName("Test1");
    item2.setId(2);
    item2.setName("Test2");
    itemList.add(item1);
    itemList.add(item2);

    element1.setProperty1("prop1");
    element1.setProperty2("prop2");
    element1.setType(2);
    element1.setItems(itemList);

    element2.setProperty1("prop11");
    element2.setProperty2("prop22");
    element2.setType(22);
    element2.setItems(itemList);

    elementList.add(element1);
    elementList.add(element2);

    elements.setElements(elementList);

    System.out.println("------- Object to XML -----------\n");
    JAXBContext jaxbContext = JAXBContext.newInstance(Elements.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(elements, System.out);

    System.out.println("\n------- XML to Object -----------\n");

    String xml = "<elements><element><items><item><id>1</id><name>Test1</name></item><item><id>2</id><name>Test2</name></item></items><property1>prop1</property1><property2>prop2</property2><type>2</type></element><element><items><item><id>1</id><name>Test1</name></item><item><id>2</id><name>Test2</name></item></items><property1>prop11</property1><property2>prop22</property2><type>22</type></element></elements>";
    StringReader reader = new StringReader(xml);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Elements elementsOut = (Elements) jaxbUnmarshaller.unmarshal(reader);
    System.out.println(elementsOut);

}

From source file:BarAdapter.java

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

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setAdapter(new BarAdapter());
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(foo, System.out);
}

From source file:webservices.simple_jaxb.UnmarshalRead.java

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