Example usage for org.dom4j.io OutputFormat setEncoding

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

Introduction

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

Prototype

public void setEncoding(String encoding) 

Source Link

Document

DOCUMENT ME!

Usage

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

License:Open Source License

/**
 * XML?// w w w. ja  v a2s . com
 * 
 * @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?/*from  w w w.j  a  v  a2s.  c om*/
 * @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();//from   ww w.  j a  v  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//w ww. j a v  a2s .  c  o m
    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  a  v 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;
}

From source file:com.cc.framework.util.SettingUtils.java

License:Open Source License

/**
 * //from www  .  ja v a2 s.  c  o  m
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/shopxx/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

public static String serialise(Document doc, String encoding) {
    String result = null;//w w  w .j  av  a  2  s . c  o m

    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(stream, mapXMLEncodingToJava(encoding));
        org.apache.xml.serialize.OutputFormat format = new org.apache.xml.serialize.OutputFormat(doc);
        format.setEncoding(encoding);
        format.setPreserveSpace(true);
        format.setPreserveEmptyAttributes(true);
        XMLSerializer output = new XMLSerializer(format);
        output.setNamespaces(true);
        output.setOutputCharStream(writer);
        output.serialize(doc);
        result = stream.toString(mapXMLEncodingToJava(encoding));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:com.dp2345.util.SettingUtils.java

License:Open Source License

/**
 * // w w w . j a  va2s. com
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile();
        Document document = new SAXReader().read(dp2345XmlFile);
        List<Element> elements = document.selectNodes("/dp2345/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(dp2345XmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java

private static void writePackageInfoZipEntry(final SoundPackage soundPackage, final ZipOutputStream out)
        throws UnsupportedEncodingException, IOException {
    // write xml to temporary byte array, because of stripping problems, when
    // directly writing to encrypted stream
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(ENCODING);
    XMLWriter writer = new XMLWriter(bOut, format);
    writer.setEscapeText(true);//  w  ww . j a  va2  s  .  co  m
    writer.write(createPackageInfoXml(soundPackage));
    writer.close();

    // write temporary byte array to encrypet zip entry
    ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray());
    writeZipEntry(PACKAGE_INFO, out, bIn);
}

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;/*from w  ww . ja v  a 2  s .com*/
        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;
            }
        }
    }
}