Example usage for org.dom4j.io XMLWriter flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the underlying Writer

Usage

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

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?/*from  ww  w .j a  v a 2  s .c  o m*/
 *
 *
 *
 * ??Schema
 *
 *
 *
 * @param document
 *            XML
 * @param outWriter
 *            
 *
 *
 *
 * @param encoding
 *            ?
 * @throws XMLDocException
 *             
 *
 *
 * @throws BaseException
 *
 */
public static void toXML(Document document, java.io.Writer outWriter, String encoding) throws BaseException {
    //
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    // ?
    outformat.setEncoding(encoding);
    XMLWriter xmlWriter = null;
    try {
        xmlWriter = new XMLWriter(outWriter, 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

/**
 * XMLDocumentjava.io.Writer?/*www  .j  ava  2  s . com*/
 *
 *
 *
 * ??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  a  2 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.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * This method will generate XML file in a StringBuffer based on the given
 * Dom4j object./*from   w  ww .  ja va2  s.  c  o  m*/
 * 
 * @param xmlObj
 *            Object
 * @param encoding
 *            String
 * @throws IOException
 * @return StringBuffer
 * @throws BaseException
 */
public static StringBuffer generateXMLStringBuffer(Object xmlObj, String encoding) throws BaseException {
    StringWriter writer = new StringWriter();
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    // ?
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    outformat.setEncoding(encoding);

    // dom4j ?OBJECT
    XMLWriter xmlWriter = null;
    xmlWriter = new XMLWriter(writer, outformat);

    try {
        xmlWriter.write(xmlObj);
        xmlWriter.flush();
    } catch (Exception ex) {
        throw new BaseException("UTIL-0002", ex);
    }

    return writer.getBuffer();
}

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

License:Open Source License

/**
 * XML?//from  w  ww .ja  v a 2  s. c om
 * 
 * @param xmlObj
 * @param encoding
 * @param filename
 * @return
 * @throws BaseException
 */
public static boolean generateXMLFile(Object xmlObj, String encoding, String filename) throws BaseException {
    FileWriter writer = null;
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    // ?
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    outformat.setEncoding(encoding);

    // dom4j ?OBJECT
    try {
        writer = new FileWriter(filename);
        XMLWriter xmlWriter = null;
        xmlWriter = new XMLWriter(writer, outformat);
        xmlWriter.write(xmlObj);
        xmlWriter.flush();
    } catch (Exception ex) {
        throw new BaseException("UTIL-0004", ex);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e) {
            }
    }

    return true;
}

From source file:com.brick.util.nciic.NciicUtil.java

/**
 * ?XML//from w  w w  . j av a 2 s . c  o m
 * @param text
 * @return
 * @throws Exception
 */
public static List<NciicEntity> readResult(String text) throws Exception {
    List<NciicEntity> resultList = new ArrayList<NciicEntity>();
    NciicEntity result = null;
    Document d;
    XMLWriter writer = null;
    try {
        //SAXReader reader = new SAXReader();
        //d = reader.read(new File("d:/test/testxml.xml"));
        d = DocumentHelper.parseText(text);
        String dateStr = DateUtil.dateToString(new Date(), "[yyyy-MM-dd][HH-mm]");
        File xmlPath = new File(XML_PATH);
        if (!xmlPath.exists()) {
            xmlPath.mkdirs();
        }
        File xpPath = new File(XP_PATH);
        if (!xpPath.exists()) {
            xpPath.mkdirs();
        }
        writer = new XMLWriter(new FileOutputStream(new File(xmlPath, dateStr + ".xml")));
        writer.write(d);
        writer.flush();
        writer.close();
        writer = null;

        Element root = d.getRootElement();
        List<Element> allResult = root.elements("ROW");
        Element input = null;
        List<Element> output = null;
        String result_msg = null;
        for (Element element : allResult) {
            result = new NciicEntity();
            result_msg = null;
            input = element.element("INPUT");
            result.setGmsfhm(input.element("gmsfhm").getText());
            result.setXm(input.element("xm").getText());
            output = element.element("OUTPUT").elements("ITEM");
            for (Element out_element : output) {
                if (out_element.element("result_gmsfhm") != null) {
                    result.setResult_gmsfhm(out_element.element("result_gmsfhm").getText());
                }
                if (out_element.element("result_xm") != null) {
                    result.setResult_xm(out_element.element("result_xm").getText());
                }
                if (out_element.element("errormesage") != null) {
                    result.setError_msg(out_element.element("errormesage").getText());
                }
                if (out_element.element("errormesagecol") != null) {
                    result.setError_msg_col(out_element.element("errormesagecol").getText());
                }
                if (out_element.element("xp") != null) {
                    result.setXp(out_element.element("xp").getText());
                }
                if (!StringUtils.isEmpty(result.getXp())) {
                    try {
                        File f = new File(xpPath, result.getGmsfhm() + "-" + result.getXm() + ".jpg");
                        BufferedImage img = ImageIO.read(
                                new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(result.getXp())));
                        ImageIO.write(img, "jpg", f);
                        result.setXp_file(f.getPath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            if ("".equals(result.getResult_gmsfhm()) && "".equals(result.getResult_xm())) {
                result_msg = "";
            } else {
                if ("?".equals(result.getResult_gmsfhm())) {
                    result_msg = "???";
                } else if ("?".equals(result.getResult_xm())) {
                    result_msg = "???";
                } else if (!StringUtils.isEmpty(result.getError_msg())) {
                    result_msg = result.getError_msg();
                    if (!StringUtils.isEmpty(result.getError_msg_col())) {
                        result_msg += "(" + result.getError_msg_col() + ")";
                    }
                }
            }
            result.setResult_msg(result_msg);
            resultList.add(result);
        }
        return resultList;
    } catch (Exception e) {
        throw e;
    } finally {
        if (writer != null) {
            writer.flush();
            writer.close();
            writer = null;
        }
    }
}

From source file:com.bullx.demo.xml.XMLParser.java

License:Open Source License

public static void bookListToXML(List<Book> books) {
    Document document = DocumentHelper.createDocument();
    // XMLbooks/*from   w  ww . j  a v a 2  s  . c om*/
    Element booksElement = document.addElement("books");
    //  
    booksElement.addComment("This is a test for dom4j, liubida, 2012.8.11");

    for (Book book : books) {
        // 
        Element bookElement = booksElement.addElement("book");
        // : show
        bookElement.addAttribute("show", book.getShow() ? "yes" : "no");
        // title
        bookElement.addElement("title").setText(book.getTitle());
        // express
        bookElement.addElement("express").setText(book.getExpress());
    }

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    StringWriter out = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(out, format);
    try {
        xmlWriter.write(document);
        xmlWriter.flush();
        String s = out.toString();
        System.out.println(s);
        Log.info("xml done!");
    } catch (Exception e) {
        Log.error("xml error!");
    } finally {
        try {
            if (null != xmlWriter) {
                xmlWriter.close();
            }
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.bullx.utils.I2Util.java

License:Open Source License

public static String prettyXML(Document document) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    StringWriter out = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(out, format);
    try {/*ww  w.ja  v  a2 s .co m*/
        xmlWriter.write(document);
        xmlWriter.flush();
        return out.toString();
    } catch (Exception e) {
        Log.error(e.getMessage());
    } finally {
        try {
            if (null != xmlWriter) {
                xmlWriter.close();
            }
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            Log.error(e.getMessage());
        }
    }
    return null;
}

From source file:com.cladonia.xml.ExchangerDocument.java

License:Open Source License

private void writeText() {
    if (DEBUG)//from  www .  j  a  va 2 s .co m
        System.out.println("ExchangerDocument.writeText()");
    try {
        XElement root = getRoot();
        if (root != null) {
            elementNames = new Hashtable();
            attributeNames = new Hashtable();
            namespaces = new Vector();

            setNames(elementNames, attributeNames, namespaces, getRoot());
        }

        //         System.out.println( document.asXML());

        StringWriter writer = new StringWriter();
        ExchangerOutputFormat format = new ExchangerOutputFormat("", false, getEncoding());

        if (hasDeclaration()) {
            if (getStandalone() != STANDALONE_NONE) {
                format.setStandalone(getStandalone());
                format.setOmitStandalone(false);
            }

            format.setVersion(getVersion());
            format.setOmitEncoding(!hasEncoding());
            format.setSuppressDeclaration(false);
        } else {
            format.setSuppressDeclaration(true);
        }

        //         if ( !getRoot().hasContent()) {
        //            format.setExpandEmptyElements( true);
        //         }

        XMLWriter formatter = new ExchangerXMLWriter(writer, format);
        formatter.write(document);
        formatter.flush();

        text = writer.toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.cladonia.xml.XMLUtilities.java

License:Open Source License

/**
 * Writes the document to the location specified by the URL,
 * using the default XML writer!//from  www  .jav a 2s .  c o  m
 *
 * @param document the dom4j document.
 * @param url the URL of the document.
 */
public static synchronized void write(XDocument document, URL url) throws IOException {
    if (DEBUG)
        System.out.println("XMLUtilities.write( " + document + ", " + url + ")");

    File documentFile = null;

    documentFile = new File(url.getFile());

    if (documentFile != null) {
        FileOutputStream out = new FileOutputStream(documentFile);

        //      XMLWriter writer = new XMLWriter( out, format);
        XMLWriter writer = new XMLWriter(out,
                new OutputFormat(TextPreferences.getTabString(), true, document.getEncoding()));
        writer.write(document);
        writer.flush();

        out.flush();
        out.close();

    } else {
        throw new IOException(url.toExternalForm() + " (The system cannot find the path specified)");
    }
}