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.beetle.framework.resource.container.ContainerConfig.java

License:Apache License

/**
 * /*from ww  w  .  ja  v  a  2  s. c  o  m*/
 *
 * @param tagname --??
 * @param key     --??
 * @throws Exception
 */
public static void setContainValue(String tagname, String key, String value) throws Exception {
    Document doc = XMLReader.getXmlDoc(sysconfigFileName);
    Node node = doc.selectSingleNode(XMLReader.convertPath(tagname));
    if (node != null) {
        @SuppressWarnings("rawtypes")
        Iterator it = node.selectNodes("item").iterator();
        while (it.hasNext()) {
            Element e = (Element) it.next();
            String id = e.valueOf("@name");
            if (id != null && id.equals(key)) {
                e.addAttribute("value", value);
                break;
            }
        }
    }
    File f = new File(sysconfigFileName);
    if (f.exists()) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        FileOutputStream fos = new FileOutputStream(f);
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.close();
    } else {
        AppLogger.getInstance(ContainerConfig.class).error("??jarxml");
    }
}

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

License:Open Source License

/**
 * XMLDocumentjava.io.Writer?//from www.jav 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?//from   w w w. ja va  2 s .c o  m
 *
 *
 *
 * ??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.  j a  va  2s .  c om*/
    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./* w w w . j  a va2s  .co 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 w  w .  jav a 2  s . co  m
 * 
 * @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.bsoft.baseframe.baseframe_utils.beanUtils.FileUtils.java

License:Open Source License

/**
 * ?XML?/*  w ww .j a  v a2  s  . co  m*/
 * @param document 
 * @param file
 * @throws IOException
 */
public static boolean wrieteXML2Doc(Document document, File file) {
    boolean isCreate = false;
    try {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();// 
        }
        if (!file.exists()) {
            isCreate = true;
            file.createNewFile();// java testData.java
        } else {
            isCreate = false;
        }
        //?XML??
        if (isCreate) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8"); //
            //document
            XMLWriter writer = new XMLWriter(new FileWriter(file), format);
            writer.write(document);
            writer.close();
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

public void output(Writer writer, RuleTemplateManager ruleTemplateManager) throws Exception {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(Constants.DEFAULT_CHARSET);

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.startDocument();/*w w w .  j  av  a2 s  .  com*/

    Element rootElement = DocumentHelper.createElement("RuleSet");
    rootElement.addAttribute("version", ruleTemplateManager.getVersion());
    xmlWriter.writeOpen(rootElement);

    OutputContext context = new OutputContext();
    outputPackageInfos(xmlWriter, ruleTemplateManager, context);

    for (RuleTemplate ruleTemplate : ruleTemplateManager.getRuleTemplates()) {
        // PropertyDataType?
        // if (ruleTemplate.isAbstract()
        // && ruleTemplate.getSubRuleTemplates().length == 0) {
        // continue;
        // }
        outputRuleTemplate(xmlWriter, ruleTemplate, context);
    }

    xmlWriter.writeClose(rootElement);
    xmlWriter.endDocument();
    xmlWriter.close();
}

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  av a2s.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 {//from w w w . j av a  2s.c  o  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;
}