Example usage for org.dom4j.io OutputFormat OutputFormat

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

Introduction

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

Prototype

public OutputFormat() 

Source Link

Document

Creates an OutputFormat with no additional whitespace (indent or new lines) added.

Usage

From source file:W3cDocument.java

License:Open Source License

public static String getXml(org.w3c.dom.Document w3cDoc, String encoding) {
    try {//from ww w.j  a v a2s . c  o m
        org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();
        org.dom4j.Document dom4jDoc = xmlReader.read(w3cDoc);

        //?
        OutputFormat format = new OutputFormat();//("    ", true);
        //?
        format.setEncoding(encoding);
        //format.setOmitEncoding(true);
        format.setSuppressDeclaration(true);
        //xml
        StringWriter out = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(out, format);
        xmlWriter.setEscapeText(true);

        //?doc
        xmlWriter.write(dom4jDoc);
        xmlWriter.flush();
        //??printWriter
        String xml = out.toString();
        out.close();
        return xml;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:android.apn.androidpn.server.xmpp.net.Connection.java

License:Open Source License

/**
 * Delivers the packet to this connection (without checking the recipient).
 * /*  www. j  av  a2 s.c  om*/
 * @param packet the packet to deliver
 */
public void deliver(Packet packet) {
    log.debug("SENT: " + packet.toXML());
    if (!isClosed()) {
        IoBuffer buffer = IoBuffer.allocate(4096);
        buffer.setAutoExpand(true);

        boolean errorDelivering = false;
        try {
            XMLWriter xmlSerializer = new XMLWriter(new IoBufferWriter(buffer, (CharsetEncoder) encoder.get()),
                    new OutputFormat());
            xmlSerializer.write(packet.getElement());
            xmlSerializer.flush();
            buffer.flip();
            ioSession.write(buffer);
        } catch (Exception e) {
            log.debug("Connection: Error delivering packet" + "\n" + this.toString(), e);
            errorDelivering = true;
        }
        if (errorDelivering) {
            close();
        } else {
            session.incrementServerPacketCount();
        }
    }
}

From source file:bookmarks.BookmarkXML.java

License:Open Source License

/**
 * /*  w w  w .j  a  va2 s.  co  m*/
 * @return
 */
public String xml_out() {
    // return stripNonValidXMLCharacters(xmlDoc.asXML());

    OutputFormat outFormat = new OutputFormat();
    outFormat.setEncoding(Utilities.REQUEST_ENCODING);
    StringWriter writer = new StringWriter();
    XMLWriter out = new XMLWriter(writer, outFormat);
    try {
        out.write(xmlDoc);
    } catch (Exception e) {

    }
    String s = writer.toString();
    return s;
}

From source file:com.alibaba.citrus.springext.util.ConvertToUnqualifiedStyle.java

License:Open Source License

private static void writeDocument(Document doc, OutputStream stream) throws IOException {
    String charset = "UTF-8";
    Writer writer = new OutputStreamWriter(stream, charset);

    OutputFormat format = new OutputFormat();

    format.setEncoding(charset);//w ww  .  j  ava  2  s .  c  om

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(doc);
    xmlWriter.flush();
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?/*w w w . j a  v  a 2  s .  c om*/
 *
 *
 *
 * ??Schema
 *
 *
 *
 * @param document
 *            XML
 * @param outStream
 *            
 *
 *
 *
 * @param encoding
 *            ?
 * @throws XMLDocException
 *             
 *
 *
 * @throws BaseException
 *
 */
public static void toXML(Document document, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    //
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(true);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

public static void element2XML(Element element, java.io.OutputStream outStream, String encoding)
        throws BaseException {
    ///*from  ww w .jav  a2  s  .  c  o m*/
    OutputFormat outformat = new OutputFormat();
    outformat.setIndentSize(0);
    outformat.setNewlines(false);
    outformat.setTrimText(true);

    // OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(new OutputStreamWriter(outStream), outformat);
        xmlWriter.write(element);
        xmlWriter.flush();
    } catch (IOException ex) {
        throw new BaseException("UTIL-0001", ex);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (IOException ex) {
            }
        }
    }
}

From source file:com.ewcms.content.particular.web.ProjectBasicAction.java

License:Open Source License

public void exportXML() {
    if (getSelections() != null && getSelections().size() > 0) {
        ServletOutputStream out = null;// ww  w  . ja  v a 2 s.  c  o m
        try {
            Document document = particularFac.exportXml(getSelections());

            StringWriter stringWriter = new StringWriter();

            OutputFormat xmlFormat = new OutputFormat();
            xmlFormat.setEncoding("UTF-8");
            XMLWriter xmlWriter = new XMLWriter(stringWriter, xmlFormat);
            xmlWriter.write(document);
            xmlWriter.flush();
            xmlWriter.close();

            HttpServletResponse resp = Struts2Util.getResponse();
            out = resp.getOutputStream();
            resp.setCharacterEncoding("UTF-8");
            resp.setContentType("text/xml; charset=UTF-8");
            resp.addHeader("Content-Disposition", "attachment; filename=xmjbxx.xml");
            out.write(stringWriter.toString().getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
                out = null;
            }
        }
    }
}

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

License:Apache License

public static void createDoument(Document doc, String filename, String encoding, boolean createPrettyPrint) {
    OutputFormat format = null;// w ww . java 2s  . c  o m
    if (createPrettyPrint) {
        format = OutputFormat.createPrettyPrint();
    } else {
        format = new OutputFormat();
    }
    format.setEncoding(encoding);
    createDoument(doc, filename, format);
}

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

License:Apache License

/**
 * ??//from   ww  w.j  av  a2s  .c om
 * 
 * @param doc
 *            Document
 * @param encoding
 *            String
 * @return byte[]
 * @throws Exception
 */
public static byte[] getBytesFromDocument(Document doc, String encoding) {
    OutputFormat format = new OutputFormat();
    format.setEncoding(encoding);
    return getBytesFromDocument(doc, format);
}

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  www  .  j a va  2s. c o m*/
    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();
}