【问题标题】:From JSON to XML and back in Java从 JSON 到 XML 再回到 Java
【发布时间】:2014-06-09 07:47:22
【问题描述】:

将 XML 转换为 JSON 非常简单。 XML 属性变成字符串值,XML 元素变成 JSON 对象。 XML 的命名约定比 JSON 更严格。回去的路比较复杂。如果在 Java 中工作,有没有办法在格式之间可靠地转换?

【问题讨论】:

  • 是否有必须使用的架构?定义“可靠”。
  • 可重复,来回结果相同。所以 X -> J -> X2 与 X === X2。使用模式,您可以补充 JSON 缺少的信息(例如,什么变成属性和什么元素),没有它是每个人的猜测
  • json-rpc 对你有用
  • @MozenRath 怎么样?没有辅助信息 JSON -> XML 很麻烦
  • 您只需要确定您希望 json 成员成为 xml 中的属性或子元素的天气。这是由您的架构/dtd 驱动的,您必须基于此转换元素。

标签: java xml json


【解决方案1】:

当您处理 bean 时,两个库让您的生活变得轻松:

使用 bean 作为 JSON 和 XML 之间简单的权威格式转换。使用此示例作为参考:

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;

    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.annotation.XmlRootElement;

    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;

    @XmlRootElement(name = "Fruit")
    public class Fruit {

        public final static String  XML_FILE    = "fruit.xml";
        public final static String  JSON_FILE   = "fruit.json";

        public static Fruit fromJson(InputStream in) {
            Gson gson = new GsonBuilder().create();
            Fruit result = gson.fromJson(new InputStreamReader(in), Fruit.class);
            return result;
        }

        public static Fruit fromXML(InputStream in) throws Exception {
            JAXBContext context = JAXBContext.newInstance(Fruit.class);
            Unmarshaller um = context.createUnmarshaller();
            return (Fruit) um.unmarshal(in);
        }

        public static void main(String[] args) throws Exception {

            Fruit f = new Fruit("Apple", "Red", "Sweet");
            Fruit f2 = new Fruit("Durian", "White", "Don't ask");

            System.out.println(f.toXML());
            System.out.println(f2.toJSON());

            f.saveXML(new FileOutputStream(new File(XML_FILE)));
            f2.saveJSON(new FileOutputStream(new File(JSON_FILE)));

            Fruit f3 = Fruit.fromXML(new FileInputStream(new File(XML_FILE)));
            System.out.println(f3.toJSON());

            Fruit f4 = Fruit.fromJson(new FileInputStream(new File(JSON_FILE)));
            System.out.println(f4.toXML());

        }

        private String  name;
        private String  color;
        private String  taste;

        public Fruit() {
            // Default constructor
        }

        public Fruit(final String name, final String color, final String taste) {
            this.name = name;
            this.color = color;
            this.taste = taste;
        }

        /**
         * @return the color
         */
        public final String getColor() {
            return this.color;
        }

        /**
         * @return the name
         */
        public final String getName() {
            return this.name;
        }

        /**
         * @return the taste
         */
        public final String getTaste() {
            return this.taste;
        }

        public void saveJSON(OutputStream out) throws IOException {
            GsonBuilder gb = new GsonBuilder();
            gb.setPrettyPrinting();
            gb.disableHtmlEscaping();
            Gson gson = gb.create();
            PrintWriter writer = new PrintWriter(out);
            gson.toJson(this, writer);
            writer.flush();
            writer.close();
        }

        public void saveXML(OutputStream out) throws Exception {
            JAXBContext context = JAXBContext.newInstance(Fruit.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(this, out);
        }

        /**
         * @param color
         *            the color to set
         */
        public final void setColor(String color) {
            this.color = color;
        }

        /**
         * @param name
         *            the name to set
         */
        public final void setName(String name) {
            this.name = name;
        }

        /**
         * @param taste
         *            the taste to set
         */
        public final void setTaste(String taste) {
            this.taste = taste;
        }

        public String toJSON() throws IOException {
            GsonBuilder gb = new GsonBuilder();
            gb.setPrettyPrinting();
            gb.disableHtmlEscaping();
            Gson gson = gb.create();
            return gson.toJson(this, Fruit.class);
        }

        public String toXML() throws Exception {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            JAXBContext context = JAXBContext.newInstance(Fruit.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(this, out);
            return out.toString();
        }

    }

【讨论】:

    【解决方案2】:

    Underscore-java 可以将 xml 转换为 json 并返回。有方法U.xmlToJson(xml)U.jsonToXml(json)。我是项目的维护者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 2015-12-06
      相关资源
      最近更新 更多