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 JAXBException {
    JAXBContext context = JAXBContext.newInstance(Vehicals.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Vehicals vehicals = new Vehicals();

    List<Car> cars = new ArrayList<Car>();
    Car c = new Car();
    c.setName("Mercedes");
    cars.add(c);/*  w  w  w.ja  v a2s. c  om*/

    c = new Car();
    c.setName("BMW");
    cars.add(c);
    vehicals.setCar(cars);
    m.marshal(vehicals, System.out);
}

From source file:example.Main.java

public static void main(String[] args) throws Exception {
    Class[] classes = new Class[3];
    classes[0] = A.class;
    classes[1] = B.class;
    classes[2] = C.class;

    JAXBContext jc = JAXBContext.newInstance(classes);

    JAXBIntrospector ji = jc.createJAXBIntrospector();
    Map<QName, Class> classByQName = new HashMap<QName, Class>(classes.length);
    for (Class clazz : classes) {
        QName qName = ji.getElementName(clazz.newInstance());
        if (null != qName) {
            classByQName.put(qName, clazz);
        }/*w w  w.j  av a2 s  .c  om*/
    }

    QName qName = new QName("http://www.example.com", "EH");
    System.out.println(classByQName.get(qName));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Main.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    String code = "<book><title>Harry Potter</title></book>";
    StreamSource source = new StreamSource(new StringReader(code));
    JAXBElement<Main> jaxbElement = unmarshaller.unmarshal(source, Main.class);
}

From source file:Main.java

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

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

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("customer.xsd"));

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

    Customer customer = new Customer();
    // populate the customer object
    JAXBSource source = new JAXBSource(jc, customer);
    schema.newValidator().validate(source);
}

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);//w  w w  .  ja va 2  s  .co  m
    System.out.println(o2);
}

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);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
    writer.setDefaultNamespace("http://www.java2s.com");

    JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    writer.writeStartDocument();//from  w w w  .  j  av  a 2  s  . co m
    writer.writeStartElement("http://www.java2s.com", "Import");
    writer.writeNamespace("", "http://www.java2s.com");
    writer.writeStartElement("WorkSets");

    m.marshal(new WorkSet(), writer);
    m.marshal(new WorkSet(), writer);

    writer.writeEndDocument();
    writer.close();
}

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(PayTypeList.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    PayTypeList paymentType = new PayTypeList();

    List<String> paymentTypes = new ArrayList<String>();
    paymentTypes.add("one");
    paymentTypes.add("two");
    paymentTypes.add("three");
    paymentType.setPayType(paymentTypes);

    m.marshal(paymentType, System.out);
}