String转XML

Posted 喻雨晨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了String转XML相关的知识,希望对你有一定的参考价值。

 /**
     * 
     * 格式化String为Xml
     * 
     * @param inputXML
     * @return
     * @throws Exception
     */
    public static String formatXml(String inputXML) throws Exception {
        String xml = null;
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        org.dom4j.Document document = reader.read(new StringReader(inputXML));
        try {
            if (document != null) {
                StringWriter stringWriter = new StringWriter();
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setNewLineAfterDeclaration(false);
                writer = new XMLWriter(stringWriter, format);
                writer.write(document);
                writer.flush();
                xml = stringWriter.getBuffer().toString();
            }
        } finally {
            if (writer != null) {
                try {
                    writer.close();

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

 

以上是关于String转XML的主要内容,如果未能解决你的问题,请参考以下文章

条件片段和导航重用

通过按钮更改 viewpager 片段 Click

String转XML

php数组转xml

java中XML操作:xml与string互转读取XML文档节点及对XML节点增删改查

[PHP] xml转对象函数simplexml_load_string