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.iterzp.momo.utils.SettingUtils.java

License:Open Source License

/**
 * //  w  ww.  j av a2  s.c  om
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.MOMO_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/momo/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.jaspersoft.jasperserver.export.ExporterImpl.java

License:Open Source License

protected void writeIndexDocument(Document indexDocument) {
    OutputStream indexOut = getIndexOutput();
    boolean closeIndexOut = true;
    try {/* w  w  w.j a v  a2  s .co m*/
        OutputFormat format = new OutputFormat();
        format.setEncoding(getCharacterEncoding());
        XMLWriter writer = new XMLWriter(indexOut, format);
        writer.write(indexDocument);

        closeIndexOut = false;
        indexOut.close();
    } catch (IOException e) {
        log.error(e);
        throw new JSExceptionWrapper(e);
    } finally {
        if (closeIndexOut) {
            try {
                indexOut.close();
            } catch (IOException e) {
                log.error("Error while closing index output", e);
            }
        }
    }
}

From source file:com.jiangnan.es.orm.mybatis.util.MybatisMapperXmlGenerator.java

License:Apache License

/**
 * /*from   ww w  .  j a v  a2  s  . c  o m*/
 * @param document
 * @throws IOException
 */
private void write(Document document) throws IOException {
    XMLWriter xmlWriter = null;
    try {
        FileWriter writer = new FileWriter(new File(this.exportFileName));
        OutputFormat format = new OutputFormat();
        format.setEncoding("UTF-8");
        //? 
        format.setIndent(true);
        format.setIndent("    ");
        //? 
        format.setNewlines(true);
        xmlWriter = new XMLWriter(writer, format);
        xmlWriter.write(document);
    } finally {
        if (null != xmlWriter) {
            xmlWriter.close();
        }
    }
}

From source file:com.lafengmaker.tool.util.XMLDataUtil.java

License:Open Source License

/**
 * Save to xml file//from   w w w .ja va2s  . c o  m
 * @param dom
 * @param sFilePathName
 * @param encode
 * @return
 * @throws CommonException
 */
public static boolean saveXML(Document dom, String sFilePathName, String encode) throws Exception {

    File file = new File(sFilePathName);

    try {
        OutputFormat format = OutputFormat.createPrettyPrint();
        FileOutputStream out = new FileOutputStream(file);
        // if(!encode.equals(ENCODE_UTF_8)){
        if (encode != null) {
            format.setEncoding(encode);
        }

        // format.setTrimText(true);
        XMLWriter xmlWriter = new XMLWriter(out, format);
        xmlWriter.write(dom);
        xmlWriter.flush();
        xmlWriter.close();
        return true;
    } catch (Exception e) {
        throw new RuntimeException("XMLDATAUTIL-SAVE_DOCUMENT-001", e);
    }

}

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

License:Open Source License

public static void set(Setting setting) {
    try {// w ww. j  a v a2s  . c om
        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.liveneo.plat.web.action.MakeLicfile.java

public static void createXmlFile(String fileName, Document doc) {
    try {/*from   ww w  . j a  v a2  s.  c o m*/
        File testfile = new File("fileName");
        if (testfile.exists()) {
            testfile.delete();
        }
        FileWriter fileWriter = new FileWriter(fileName);
        OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
        xmlFormat.setEncoding("UTF-8");
        xmlFormat.setSuppressDeclaration(false);
        xmlFormat.setExpandEmptyElements(false);
        XMLWriter xmlWriter = new XMLWriter(fileWriter, xmlFormat);
        xmlWriter.write(doc);
        xmlWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.love320.templateparser.label.impl.LabelBeanDaoImpl.java

License:Apache License

private boolean XMLWriter() {
    try {//from   w ww .  ja  va  2  s . c  om
        OutputFormat format = OutputFormat.createPrettyPrint();//?
        format.setEncoding("UTF-8");//?
        XMLWriter output = new XMLWriter(new FileWriter(new File(configPath)), format);//?
        output.write(DOCROOT.getDocument());//
        output.close();//

        return true;
    } catch (IOException e) {
        logger.error("IOException", e);
    }
    return false;
}

From source file:com.mor.blogengine.xml.io.XmlDataSourceProvider.java

License:Open Source License

/**
 * writes in a file//from ww  w .ja v a  2 s. c  om
 *
 * @param document DOM model to write in
 * @param pOutputFile output file
 * @throws java.io.IOException
 * @param pDocument
 */
boolean write(Document pDocument) throws MissingPropertyException, IncorrectPropertyValueException {
    boolean ret = false;
    try {
        OutputFormat format = new OutputFormat();
        format.setEncoding(getFileEncoding());
        XMLWriter writer;
        writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xml.getFile()),
                Charset.forName(getFileEncoding())));

        writer.write(pDocument);
        writer.close();
        ret = true;

    } catch (IOException ex) {
        trace("Error saving file..." + ex);

    }
    return ret;

}

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

/**
 * //from   w  ww.j  a  va2s . co  m
 * @param filename
 * @return
 */
public int formatXMLFile(String filename) {

    int returnValue = 0;

    try {

        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File(filename));
        XMLWriter writer = null;
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        writer = new XMLWriter(new FileWriter(new File(filename)), format);
        writer.write(document);
        writer.close();
        returnValue = 1;

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return returnValue;
}

From source file:com.orange.atk.atkUI.corecli.utils.XMLOutput.java

License:Apache License

public void generate() {
    try {/*from   w ww  . jav a  2  s  .  c o m*/
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter(new FileWriter(file), format);
        writer.write(doc);
        writer.close();
    } catch (Exception e) {
        Alert.raise(e, "While generating " + file);
    }
}