Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller marshal.

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:Main.java

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

    Root root = new Root();
    root.setFoo("Hello\rWorld");
    root.setBar("Hello\nWorld");

    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(root, System.out);
}

From source file:Main.java

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

    // Create the Object
    Main foo = new Main();
    foo.setBar("Hello World");

    // Create the Document
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.newDocument();

    // Marshal the Object to a Document
    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(foo, document);

    // Output the Document
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(System.out);
    t.transform(source, result);/*from www .j a v  a  2  s .  c  om*/
}

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    Animal tiger = new Animal();
    tiger.setType("A");
    tiger.setValue("Tiger");

    JAXBContext jaxbContext = JAXBContext.newInstance(Animal.class);
    Marshaller m = jaxbContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(tiger, System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jxbc = JAXBContext.newInstance(Main.class);
    Marshaller marshaller = jxbc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.marshal(new JAXBElement(new QName("root"), Main.class, new Main("test")), System.out);
}

From source file:Main.java

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

    Type type = new Type();

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

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 {
    JAXBContext jc = JAXBContext.newInstance(Response.class);

    Response response = new Response();
    response.setMessage("1 < 2");

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

From source file:Main.java

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

    Foo foo = new Foo();
    foo.setBar("Hello\nWorld");

    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 {
    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(PersonTraining.class);

    PersonTraining pt = new PersonTraining();

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