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(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 JAXBException {
    Person p = new Person();
    p.setFirstName("B");
    p.setLastName("H");
    PersonName pn = new PersonName();
    pn.setValue("L");
    p.setFriend(pn);//  www. j  a v a2 s . co m
    JAXBContext context = JAXBContext.newInstance(Person.class);
    context.createMarshaller().marshal(p, System.out);
}

From source file:Main.java

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

    Root root = new Root();
    DatatypeFactory dtf = DatatypeFactory.newInstance();
    root.month = dtf.newXMLGregorianCalendar("--11");

    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 {
    XMLInputFactory xif = XMLInputFactory.newFactory();

    FileInputStream xml = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(xml);
    xsr.nextTag(); // Advance to "Persons" tag
    xsr.nextTag(); // Advance to "Person" tag

    JAXBContext jc = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    List<Person> persons = new ArrayList<Person>();
    while (xsr.hasNext() && xsr.isStartElement()) {
        Person person = (Person) unmarshaller.unmarshal(xsr);
        persons.add(person);/*from ww w. j  av a  2 s.c  o  m*/
        xsr.nextTag();
    }

    for (Person person : persons) {
        System.out.println(person.getName());
    }
}

From source file:MarshalValidation.java

public static void main(String[] args) throws Exception {
    Person p = new Person();
    p.setFirstName("B");
    p.setLastName("H");

    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();

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

    marshaller.setSchema(schema);/*from  w  w w.  ja v a2  s .co  m*/
    marshaller.setEventHandler(new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent event) {
            System.out.println(event);
            return false;
        }
    });

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    MessageRS message = new MessageRS();
    message.setUserId(10);/*from   w  w  w. j av a  2  s .co m*/
    MessageRS.Bucket bucket1 = new MessageRS.Bucket();
    bucket1.setText("This is sample text");
    bucket1.setChannels(Arrays.asList("Test A", "Test B"));
    MessageRS.Bucket bucket2 = new MessageRS.Bucket();
    bucket2.setText("Some more text");
    bucket2.setChannels(Arrays.asList("1", "2"));
    message.setBuckets(Arrays.asList(bucket1, bucket2));
    StringWriter writer = new StringWriter();
    JAXBContext.newInstance(MessageRS.class).createMarshaller().marshal(message, writer);
    System.out.println(writer);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Map map = new HashMap<String, String>();
    map.put("cluster", "10.200.111.111");
    map.put("cluster1", "10.200.121.111");

    Product xml = new Product();
    List<Top> top1 = new ArrayList<Top>();
    Set<String> keys = map.keySet();
    for (String key : keys) {
        Top top = new Top();
        top.setMode(key);/*from   w w  w  .j  a  va2 s.com*/
        top.setAddress((String) map.get(key));
        top1.add(top);
    }
    xml.setTop(top1);
    File file = new File("C:\\kar\\file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

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

    jaxbMarshaller.marshal(xml, file);
    jaxbMarshaller.marshal(xml, System.out);
}

From source file:DataSet.java

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

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("input.xml");
    DataSet dataSet = (DataSet) unmarshaller.unmarshal(xml);

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    Order o = new Order();
    o.setCustId(123);//from   w  w w  .  ja v a2 s  .c om
    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:Tag.java

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

    Tag tag = new Tag();
    tag.setAttribute1(Tag.Foo.A);/*from  w  ww .j a  v  a2  s  .  c om*/
    System.out.println(tag.getAttribute1());
    marshaller.marshal(tag, System.out);

    tag.setAttribute1(Tag.Foo.B);
    System.out.println(tag.getAttribute1());
    marshaller.marshal(tag, System.out);
}