Search in sources :

Example 66 with OutputFormat

use of org.dom4j.io.OutputFormat in project bw-calendar-engine by Bedework.

the class XmlTidy method main.

/**
 * @param args
 * @throws Exception
 */
public static void main(final String[] args) throws Exception {
    String text = FileUtils.readFileToString(new File(args[0]));
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setTrimText(false);
    // XMLWriter writer = new XMLWriter(System.out, format);
    XMLWriter writer = new XMLWriter(new FileOutputStream(args[1]), format);
    writer.write(DocumentHelper.parseText(text));
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter)

Example 67 with OutputFormat

use of org.dom4j.io.OutputFormat in project tutorials by eugenp.

the class Dom4JParser method generateNewDocument.

public void generateNewDocument() {
    try {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("XMLTutorials");
        Element tutorialElement = root.addElement("tutorial").addAttribute("tutId", "01");
        tutorialElement.addAttribute("type", "xml");
        tutorialElement.addElement("title").addText("XML with Dom4J");
        tutorialElement.addElement("description").addText("XML handling with Dom4J");
        tutorialElement.addElement("date").addText("14/06/2016");
        tutorialElement.addElement("author").addText("Dom4J tech writer");
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter(new File("src/test/resources/example_dom4j_new.xml")), format);
        writer.write(document);
        writer.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Element(org.dom4j.Element) FileWriter(java.io.FileWriter) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) File(java.io.File)

Example 68 with OutputFormat

use of org.dom4j.io.OutputFormat in project application by collectionspace.

the class NullResolver method serializeToBytes.

/**
 * Simple serializer helper
 * @param doc
 * @return
 * @throws IOException
 */
static byte[] serializeToBytes(Document doc) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setExpandEmptyElements(true);
    outformat.setNewlines(true);
    outformat.setIndent(false);
    XMLWriter writer = new XMLWriter(out, outformat);
    writer.write(doc);
    writer.flush();
    out.close();
    return out.toByteArray();
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLWriter(org.dom4j.io.XMLWriter)

Example 69 with OutputFormat

use of org.dom4j.io.OutputFormat in project openmeetings by apache.

the class XmlExport method toXml.

public static void toXml(Writer out, Document doc) throws Exception {
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setIndentSize(1);
    outformat.setIndent("\t");
    outformat.setEncoding(UTF_8.name());
    XMLWriter writer = new XMLWriter(out, outformat);
    writer.write(doc);
    writer.flush();
    out.flush();
    out.close();
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Example 70 with OutputFormat

use of org.dom4j.io.OutputFormat in project eclipse-cs by checkstyle.

the class XMLUtil method toByteArray.

/**
 * Creates a pretty printed representation of the document as a byte array.
 *
 * @param document
 *          the document
 * @return the document as a byte array (UTF-8)
 * @throws IOException
 *           Exception while serializing the document
 */
public static byte[] toByteArray(Document document) throws IOException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512);
    // Pretty print the document to System.out
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(byteOut, format);
    writer.write(document);
    return byteOut.toByteArray();
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4