Example usage for org.dom4j.io OutputFormat createPrettyPrint

List of usage examples for org.dom4j.io OutputFormat createPrettyPrint

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat createPrettyPrint.

Prototype

public static OutputFormat createPrettyPrint() 

Source Link

Document

A static helper method to create the default pretty printing format.

Usage

From source file:org.dentaku.gentaku.tools.cgen.xmi.XMIGenTask.java

License:Apache License

private void writeFile(Branch document, File file) throws IOException {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(System.getProperty("file.encoding"));
    format.setSuppressDeclaration(false);
    format.setExpandEmptyElements(false);

    Writer out = new FileWriter(file);
    final XMLWriter xmlWriter = new XMLWriter(out, format);
    xmlWriter.setEscapeText(false);// ww  w.j ava  2s  .co  m

    xmlWriter.write(document);
    xmlWriter.flush();
    xmlWriter.close();
}

From source file:org.dom4j.samples.applets.SimpleAppletDemo.java

License:Open Source License

/**
 * Called after init. Demonstrates the simplicity of parsing in applets.
 *///from   w ww.ja  v a2s .co m
public void start() {
    try {
        demoDocument = DocumentHelper.parseText(DEMO_XML);
        new XMLWriter(OutputFormat.createPrettyPrint()).write(demoDocument);
    } catch (DocumentException documentEx) {
        documentEx.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    demoXPath();
    repaint();
}

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

License:Open Source License

public void demoWrite() {
    try {//from ww  w. j  a va  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();
    }
}

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

License:Open Source License

public void demoModifyWrite() {
    try {//from w ww.j  a  va  2s  .  c  o m
        File inputFile = new File("xml/jaxb/primer.xml");

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

        JAXBModifier jaxbModifier = new JAXBModifier("org.dom4j.test.primer", OutputFormat.createPrettyPrint());
        jaxbModifier.setPruneElements(true);
        jaxbModifier.setOutput(outputFile);
        jaxbModifier.addObjectModifier("/purchaseOrders/purchaseOrder", new PurchaseOrderDateModifier());
        Document doc = jaxbModifier.modify(inputFile);

        System.out.println("Pruned modified document:");
        System.out.println(doc.asXML());
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.dom4j.samples.jaxp.PrettyPrintDemo.java

License:Open Source License

/** Outputs the document using JAXP */
protected void process(Document document) throws Exception {
    // load a default transformer
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();

    // use dom4j document as the source
    Source source = new DocumentSource(document);

    // use pretty print format and a buffer for the result
    OutputFormat format = OutputFormat.createPrettyPrint();
    StringWriter buffer = new StringWriter();
    Result result = new XMLResult(buffer, format);

    // now lets transform
    transformer.transform(source, result);

    String text = buffer.toString();
    System.out.println("The document is:- ");
    System.out.println(text);//from   w w w.j a  v  a2s.c  o  m
}

From source file:org.dom4j.samples.rule.SongFilter.java

License:Open Source License

public static void main(String[] args) throws Exception {
    SongFilter filter = new SongFilter();
    URL source = filter.getClass().getResource("/org/dom4j/samples/rule/Songs.xml");
    Document result = filter.filtering(new SAXReader().read(source));

    XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
    writer.setOutputStream(System.out);
    writer.write(result);/*from ww  w  .ja v a 2s  .  c  om*/

}

From source file:org.dom4j.samples.validate.SAXValidatorDemo.java

License:Open Source License

protected void validate(String url, boolean validateOnParse) throws Exception {
    println("Parsing: " + url + " with validation mode: " + validateOnParse);

    XMLErrorHandler errorHandler = new XMLErrorHandler();

    if (validateOnParse) {
        // validate as we parse
        SAXReader reader = new SAXReader(true);
        reader.setErrorHandler(errorHandler);

        try {//  w w  w.  ja va 2  s  .com
            Document document = reader.read(url);
            println("Document: " + url + " is valid!");
        } catch (DocumentException e) {
            println("Document: " + url + " is not valid");
            println("Exception: " + e);
        }
    } else {
        // parse without validating, then do that later
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);

        println("Document URI: " + document.getName());

        // now lets set a doc type if one isn't set
        DocumentType docType = document.getDocType();
        if (docType == null) {
            println("Adding an NITF doc type");
            document.addDocType("nitf", null, "nitf.dtd");
        }

        // now lets validate
        try {
            SAXValidator validator = new SAXValidator();
            validator.setErrorHandler(errorHandler);
            validator.validate(document);

            println("Document: " + url + " is valid!");
        } catch (SAXException e) {
            println("Document: " + url + " is not valid");
            println("Exception: " + e);
        }
    }

    // now lets output any errors as XML
    Element errors = errorHandler.getErrors();
    if (errors.hasContent()) {
        XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
        writer.write(errors);
    }
}

From source file:org.dom4j.samples.validate.XercesDemo.java

License:Open Source License

protected Document parse(String uri) throws Exception {
    SAXReader reader = new SAXReader();

    reader.setValidation(true);//from ww  w. j  a  va2 s  . c o m

    // specify the schema to use
    reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
            "personal.xsd");

    // add an error handler which turns any errors into XML
    XMLErrorHandler errorHandler = new XMLErrorHandler();
    reader.setErrorHandler(errorHandler);

    // now lets parse the document
    Document document = reader.read(uri);

    // now lets output the errors as XML
    XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
    writer.write(errorHandler.getErrors());

    return document;
}

From source file:org.dtolabs.rundeck.resources.format.XmlGenerator.java

License:Apache License

private void writeDocument(final Document document, final OutputStream out) throws IOException {
    // Pretty print the document
    final OutputFormat format = OutputFormat.createPrettyPrint();
    final XMLWriter writer = new XMLWriter(out, format);
    writer.write(document);//ww w.ja v  a2  s. c o m
}

From source file:org.eclipse.birt.build.GenCaseResult.java

License:Open Source License

private void genReport(String path) {

    /*/*  w  ww  . ja  va 2s  . co m*/
     * Add genDate node to report
     */

    DOMElement testElement = new DOMElement("ReportDate");
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat currentDate = new SimpleDateFormat("yyyy-MM-dd");

    String strDate = currentDate.format(cal.getTime());
    testElement.setText(strDate);
    this.rootElement.add(testElement);

    DocumentFactory factory = new DocumentFactory();
    Document doc = factory.createDocument(rootElement);
    OutputFormat format = OutputFormat.createPrettyPrint();

    try {

        XMLWriter writer = new XMLWriter(new FileWriter(this.getReportPath(path)), format);
        writer.write(doc);
        writer.close();

    } catch (Exception ex) {

        ex.printStackTrace();

    }

}