Example usage for org.dom4j.jaxb JAXBWriter startDocument

List of usage examples for org.dom4j.jaxb JAXBWriter startDocument

Introduction

In this page you can find the example usage for org.dom4j.jaxb JAXBWriter startDocument.

Prototype

public void startDocument() throws IOException, SAXException 

Source Link

Document

Start a document by writing the initial XML declaration to the output.

Usage

From source file:fr.cls.atoll.motu.library.misc.xml.XMLUtils.java

License:Open Source License

/**
 * Dom4j to intput stream./*from   www .j a va2 s . c o m*/
 * 
 * @param document the document
 * @param contextPath the context path
 * 
 * @return the input stream
 * 
 * @throws MotuExceptionBase the motu exception base
 */
public static InputStream dom4jToIntputStream(org.dom4j.Document document, String contextPath)
        throws MotuExceptionBase {
    ByteArrayInputStream byteArrayInputStream = null;
    try {
        Element root = document.getRootElement();
        JAXBWriter jaxbWriter = new JAXBWriter(contextPath);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        jaxbWriter.setOutput(byteArrayOutputStream);

        jaxbWriter.startDocument();
        jaxbWriter.writeElement(root);
        jaxbWriter.endDocument();

        byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    } catch (IOException e) {
        throw new MotuException("ERROR in XMLUtils#dom4jToIntputStream", e);
    } catch (SAXException e) {
        throw new MotuException("ERROR in XMLUtils#dom4jToIntputStream", e);
    }
    return byteArrayInputStream;

}

From source file:org.dom4j.samples.jaxb.JAXBDemo.java

License:Open Source License

public void demoWrite() {
    try {// www . j a  v  a 2s  .c o  m
        ObjectFactory factory = new ObjectFactory();

        PurchaseOrders orders = factory.createPurchaseOrders();

        // Order 1
        PurchaseOrder order = factory.createPurchaseOrder();

        USAddress billTo = factory.createUSAddress();
        billTo.setCity("Cambridge");
        billTo.setCountry("US");
        billTo.setName("Robert Smith");
        billTo.setState("MA");
        billTo.setStreet("8 Oak Avenue");
        billTo.setZip(new BigDecimal(12345));
        order.setBillTo(billTo);

        USAddress shipTo = factory.createUSAddress();
        shipTo.setCity("Cambridge");
        shipTo.setCountry("US");
        shipTo.setName("Alice Smith");
        shipTo.setState("MA");
        shipTo.setStreet("123 Maple Street");
        shipTo.setZip(new BigDecimal(12345));
        order.setShipTo(shipTo);

        Calendar orderDate = Calendar.getInstance();
        orderDate.set(2004, 06, 30);
        order.setOrderDate(orderDate);

        Items items = factory.createItems();
        order.setItems(items);

        orders.getPurchaseOrder().add(order);

        // Order 2
        PurchaseOrder order2 = factory.createPurchaseOrder();

        USAddress billTo2 = factory.createUSAddress();
        billTo2.setCity("Cambridge");
        billTo2.setCountry("US");
        billTo2.setName("Robert Smith");
        billTo2.setState("MA");
        billTo2.setStreet("8 Oak Avenue");
        billTo2.setZip(new BigDecimal(12345));
        order2.setBillTo(billTo2);

        USAddress shipTo2 = factory.createUSAddress();
        shipTo2.setCity("Cambridge");
        shipTo2.setCountry("US");
        shipTo2.setName("Alice Smith");
        shipTo2.setState("MA");
        shipTo2.setStreet("123 Maple Street");
        shipTo2.setZip(new BigDecimal(12345));
        order2.setShipTo(shipTo2);

        Calendar orderDate2 = Calendar.getInstance();
        orderDate2.set(2004, 06, 30);
        order2.setOrderDate(orderDate2);

        Items items2 = factory.createItems();
        order2.setItems(items2);

        orders.getPurchaseOrder().add(order2);

        File outputFile = new File(outputDir, "jaxbWrite.xml");

        JAXBWriter jaxbWriter = new JAXBWriter("org.dom4j.test.primer", OutputFormat.createPrettyPrint());
        jaxbWriter.setOutput(outputFile);

        jaxbWriter.startDocument();
        jaxbWriter.write(orders);
        jaxbWriter.endDocument();
    } catch (Exception e) {
        e.printStackTrace();
    }
}