1

Converting XML to JSON is quite straight forward. XML Attributes become String values and XML Elements become JSON objects. Naming conventions are stricter for XML than JSON. The way back is more complicated. If working in Java, is there a way to reliably convert between the formats?

5
  • Are there any schemas that must be used? Define "reliable". Commented Apr 24, 2014 at 6:59
  • repeatable, same result back and forth. So X -> J -> X2 with X === X2. With a schema you can supplement information to JSON that it is lacking (e.g. what becomes an attribute and what an element), without it is everybody's guess Commented Apr 24, 2014 at 7:02
  • json-rpc will work for u Commented Apr 24, 2014 at 7:20
  • @MozenRath how? Without auxiliary information JSON -> XML is troublesome Commented Apr 24, 2014 at 7:22
  • you just need to decide weather you want json members to be attributes or child elements in xml. This is driven by your schema/dtd and you will have to convert elements based on that. Commented Apr 24, 2014 at 7:26

2 Answers 2

3

When you are dealing with a bean, two libraries make your life easy:

Using the bean as authoritative format conversion between JSON and XML simple. Use this example as reference:

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

    }
Sign up to request clarification or add additional context in comments.

Comments

1

Underscore-java can convert xml to json and back. There are methods U.xmlToJson(xml) and U.jsonToXml(json). I am the maintainer of the project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.