java json 转为xml文件

Posted

tags:

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

1.jar包引用

<dependency>
          <groupId>xerces</groupId>
          <artifactId>xercesImpl</artifactId>
          <version>2.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/xom/xom -->
        <dependency>
            <groupId>xom</groupId>
            <artifactId>xom</artifactId>
            <version>1.0</version>
        </dependency> 

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();
    }

以上是关于java json 转为xml文件的主要内容,如果未能解决你的问题,请参考以下文章

java自用代码(包括:新建单线程创建文件夹及文件map转为json并将json写入txt文件剪切或改名)

json渲染xml 自定义节点

Java xml和json互相转换方法

java 怎么把map转为json

java中把json怎么转换成数组

c#一个程序把xml文件转化成json对象,怎么从这个程序中获取json对象的值