博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java json 转为xml文件
阅读量:7228 次
发布时间:2019-06-29

本文共 3097 字,大约阅读时间需要 10 分钟。

1.jar包引用

xerces
xercesImpl
2.9.1
xom
xom
1.0

2.main方法

package com.han;import org.apache.commons.io.FileUtils;import org.apache.xml.serialize.OutputFormat;import org.apache.xml.serialize.XMLSerializer;import org.w3c.dom.Document;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import net.sf.json.JSONObject;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.io.StringReader;import java.io.StringWriter;import java.io.Writer;public class XmlFormatter {        //格式化    public String format(String unformattedXml) {        try {            final Document document = parseXmlFile(unformattedXml);            OutputFormat format = new OutputFormat(document);            format.setLineWidth(65);            format.setIndenting(true);            format.setIndent(2);            Writer out = new StringWriter();            XMLSerializer serializer = new XMLSerializer(out, format);            serializer.serialize(document);            return out.toString();        } catch (IOException e) {            throw new RuntimeException(e);        }    }    private Document parseXmlFile(String in) {        try {            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();            DocumentBuilder db = dbf.newDocumentBuilder();            InputSource is = new InputSource(new StringReader(in));            return db.parse(is);        } catch (ParserConfigurationException e) {            throw new RuntimeException(e);        } catch (SAXException e) {            throw new RuntimeException(e);        } catch (IOException e) {            throw new RuntimeException(e);        }    }        //转化为String    public static String jsonToXML(String json) {        net.sf.json.xml.XMLSerializer xmlSerializer = new net.sf.json.xml.XMLSerializer();        // 根节点名称        xmlSerializer.setRootName("resource");        // 不对类型进行设置        xmlSerializer.setTypeHintsEnabled(false);        String xmlStr = "";        JSONObject jobj = JSONObject.fromObject(json);        xmlStr = xmlSerializer.write(jobj);        return xmlStr;    }    public static void main(String[] args) throws Exception{        String aa = "{\"index\":\"0\",\"title\":null,\"order\":\"0\",\"componentKey\":\"ColumnPanel\",\"layoutDetail\":[{\"aa\":\"12\"}]}";        // String original_filename= "/Users/xidehan/Downloads/aa.txt";       //String file = FileUtils.readFileToString(new File(original_filename));        String s = jsonToXML(file);        System.out.println(new XmlFormatter().format(s));        PrintWriter writer = new PrintWriter("/Users/xidehan/Downloads/resource.xml", "UTF-8");        writer.println(new XmlFormatter().format(s));        writer.close();    }

转载于:https://blog.51cto.com/13873187/2320952

你可能感兴趣的文章
【译】Css Grid VS Flexbox: 实践比较
查看>>
iOS 开发知识索引
查看>>
Linux iptables命令
查看>>
webpack的使用
查看>>
干货 | 基于Go SDK操作京东云对象存储OSS的入门指南
查看>>
D3.js入门
查看>>
一次和前端的相互甩锅的问题记录
查看>>
纯OC实现iOS DLNA投屏功能了解一下
查看>>
RxJava -- fromArray 和 Just 以及 interval
查看>>
LC #75 JS
查看>>
js正则验证代码库
查看>>
常见面试题—css实现垂直水平居中
查看>>
lc682. Baseball Game
查看>>
重学前端-css选择器
查看>>
iOS开发之扫描二维码
查看>>
Android黑科技: 快速找到view所在的xml文件
查看>>
linux分区方案
查看>>
003-Java技术体系
查看>>
超轻量模板引擎
查看>>
JavaScript 复习之 Object对象的相关方法
查看>>