Example usage for org.dom4j.io XMLWriter XMLWriter

List of usage examples for org.dom4j.io XMLWriter XMLWriter

Introduction

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

Prototype

public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException 

Source Link

Usage

From source file:com.glaf.core.util.Dom4jUtils.java

License:Apache License

/**
 * ??//  w w  w . j av  a2s . com
 * 
 * @param doc
 *            Document
 * @param format
 *            OutputFormat
 * @return byte[]
 * @throws Exception
 */
public static byte[] getBytesFromDocument(Document doc, OutputFormat format) {
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    XMLWriter writer = null;
    byte[] data = null;
    try {
        baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos);
        writer = new XMLWriter(bos, format);
        writer.write(doc);
        if (baos != null) {
            data = baos.toByteArray();
        }
        bos.close();
        baos.close();
        baos = null;
        return data;
    } catch (IOException ex) {
        bos = null;
        writer = null;
        throw new RuntimeException(ex);
    } finally {
        try {
            if (writer != null) {
                writer.close();
                writer = null;
            }
            if (bos != null) {
                bos.close();
                bos = null;
            }
            if (baos != null) {
                baos.close();
                baos = null;
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:com.globalsight.cxe.util.Dom4jUtil.java

License:Apache License

public static String formatXML(Document document, String charset) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(charset);//from w ww  .ja va2s  .  c  o m
    StringWriter sw = new StringWriter();
    XMLWriter xw = new XMLWriter(sw, format);
    try {
        xw.write(document);
        xw.flush();
        xw.close();
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        return document.asXML();
    }

    return sw.toString();
}

From source file:com.globalsight.diplomat.util.XmlUtil.java

License:Apache License

public static String format(String xml, EntityResolver entityResolver) {
    XmlParser parser = XmlParser.hire();
    if (entityResolver != null) {
        parser.setEntityResolver(entityResolver);
    }/*from  w w w .  j a va2 s.  c  om*/
    org.dom4j.Document dom = parser.parseXml(xml);
    OutputFormat format = new OutputFormat();
    format.setNewlines(true);
    format.setIndent(true);

    StringWriter writer = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    try {
        xmlWriter.write(dom);
        xmlWriter.close();
    } catch (IOException e) {
        return xml;
    } finally {
        try {
            xmlWriter.close();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

    return writer.getBuffer().toString();
}

From source file:com.globalsight.everest.tm.exporter.TmxWriter.java

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without the
 * top-level tag.//from  ww w . ja  v a  2 s .c om
 */
private static String getInnerXml(Element p_node, OutputFormat outputFormat) {
    StringBuffer result = new StringBuffer();

    List content = p_node.content();

    for (int i = 0, max = content.size(); i < max; i++) {
        Node node = (Node) content.get(i);

        // Work around a specific behaviour of DOM4J text nodes:
        // The text node asXML() returns the plain Unicode string,
        // so we need to encode entities manually.
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(EditUtil.encodeXmlEntities(node.getText()));
        } else {
            // Note: DOM4J's node.asXML() constructs the same 2 objects.
            StringWriter out = new StringWriter();
            XMLWriter writer = new XMLWriter(out, outputFormat);

            try {
                writer.write(node);
            } catch (IOException ignore) {
            }

            result.append(out.toString());
        }
    }

    return result.toString();
}

From source file:com.globalsight.everest.workflow.WorkflowTemplateAdapter.java

License:Apache License

/**
 * Saves the workflow template xml to the file storage dir.
 * /*from   w ww .  j a v  a  2 s.c o  m*/
 * @param p_document
 *            - the xml document of the workflow template.
 * @param p_templateName
 *            - the workflow template name.
 * 
 */
private void saveXmlToFileStore(Document p_document, String p_templateName) {
    OutputFormat format = OutputFormat.createCompactFormat();
    XMLWriter writer = null;
    try {
        String aaa = AmbFileStoragePathUtils.getWorkflowTemplateXmlDir().getAbsolutePath();
        writer = new XMLWriter(
                new FileOutputStream(AmbFileStoragePathUtils.getWorkflowTemplateXmlDir().getAbsolutePath()
                        + File.separator + p_templateName + WorkflowConstants.SUFFIX_XML),
                format);
        writer.write(p_document);
    } catch (Exception e) {
        c_category.info("Exception occurs when saving the template xml to file storage");
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/**
 * Convert node to string/*  www  .jav a 2 s  .co  m*/
 * 
 * @param outDoc Node to be converted
 * @return String of the converted node
 * @throws IOException if the conversion fail
 */
public String docToString(Node outDoc) throws IOException {
    Writer osw = new StringWriter();
    OutputFormat opf = new OutputFormat("", false, "UTF-8");
    opf.setSuppressDeclaration(true);
    opf.setExpandEmptyElements(true);
    XMLWriter writer = new XMLWriter(osw, opf);
    writer.setEscapeText(false);
    writer.write(outDoc);
    writer.close();

    return osw.toString();
}

From source file:com.googlecode.fascinator.common.sax.SafeSAXReader.java

License:Open Source License

/**
 * Convert node to stream/* w ww .  j a  va  2s.com*/
 * 
 * @param outDoc Node to be converted
 * @param outStream output stream of the converted node
 * @throws IOException if the conversion fail
 */
public void docToStream(Node outDoc, OutputStream outStream) throws IOException {
    OutputFormat opf = new OutputFormat("", false, "UTF-8");
    opf.setSuppressDeclaration(true);
    opf.setExpandEmptyElements(true);
    XMLWriter writer = new XMLWriter(outStream, opf);
    writer.setEscapeText(false);
    writer.write(outDoc);
    writer.close();
}

From source file:com.googlecode.starflow.engine.xml.XmlFormat.java

License:Apache License

/**
 * dom4j'pretty'??dom./* w  w  w. j ava 2  s.  com*/
 * 
 * @param doc
 *            ??dom.
 * @param encoding
 *            ??.
 * @return ??XML.null,???.
 */
public static String getPrettyString(Document doc, String encoding) {
    StringWriter writer = new StringWriter();
    OutputFormat format = OutputFormat.createPrettyPrint();
    if (encoding == null || "".equals(encoding.trim())) {
        encoding = "GBK";
    }
    format.setEncoding(encoding);
    XMLWriter xmlwriter = new XMLWriter(writer, format);

    try {
        xmlwriter.write(doc);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return writer.toString();
}

From source file:com.haulmont.bali.util.Dom4j.java

License:Apache License

public static void writeDocument(Document doc, boolean prettyPrint, Writer writer) {
    XMLWriter xmlWriter;//from  w ww  . j a  v  a  2 s. c om
    try {
        if (prettyPrint) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            xmlWriter = new XMLWriter(writer, format);
        } else {
            xmlWriter = new XMLWriter(writer);
        }
        xmlWriter.write(doc);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write XML", e);
    }
}

From source file:com.haulmont.bali.util.Dom4j.java

License:Apache License

public static void writeDocument(Document doc, boolean prettyPrint, OutputStream stream) {
    XMLWriter xmlWriter;//from  ww  w . j  a v a  2s  .  c o m
    try {
        if (prettyPrint) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            xmlWriter = new XMLWriter(stream, format);
        } else {
            xmlWriter = new XMLWriter(stream);
        }
        xmlWriter.write(doc);
    } catch (IOException e) {
        throw new RuntimeException("Unable to write XML", e);
    }
}