I've done the same using JAXB to convert my xml to an object, and passing the object to gson. I know it takes one additional step, but that worked convenient for me. Upon converting xml to jaxb see also: Use JAXB to create Object from XML String

Answer from Michallis on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 58008569 › converting-xml-to-json-using-gson-in-pi-java-mapping
Converting XML to JSON using GSON in PI Java Mapping - Stack Overflow
May 11, 2020 - package xmlgsonjson; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.sap.aii.mapping.api.AbstractTransformation; import com.sap.aii.mapping.api.StreamTransformationException; import com.sap.aii.mapping.api.TransformationInput; import com.sap.aii.mapping.api.TransformationOutput; public class MakeItJSONByGSON extends AbstractTransformation { public String targetfile =""; public MakeItJSONByGSON() {} public void transfor
Discussions

xml to json - Google Gson example - Q&A Forum - Q&A - Temenos Community
I have a use case where I receive server output, from a Dynamic Data service call, in xml format and would like to convert it to JSON format. Rather than hand craft a converter I was hoping to use a library and noticed that the Google Gson library is part of the 3rd Party Java Libraries included ... More on community.avoka.com
🌐 community.avoka.com
From JSON to XML and back in Java - Stack Overflow
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 More on stackoverflow.com
🌐 stackoverflow.com
How to convert an xml into a json using gson library
I have to convert a XML using gson library into a JSON i haven´t found how to do it using gson library(java) Answer You could use Jackson to do this: import these libraries: then do this in your class: More on java.tutorialink.com
🌐 java.tutorialink.com
1
xml to json - Google Gson example - Q&A Forum - Q&A - Avoka Community
I have a use case where I receive server output, from a Dynamic Data service call, in xml format and would like to convert it to JSON format. Rather than hand craft a converter I was hoping to use a library and noticed that the Google Gson library is part of the 3rd Party Java Libraries included ... More on community.avoka.com
🌐 community.avoka.com
🌐
Coderanch
coderanch.com › t › 672512 › java › xml-json-gson
xml to json using gson (Java in General forum at Coderanch)
November 8, 2016 - How to read the xml then generate to json using gson?. Every content will be start with and end with .
🌐
GitHub
github.com › stanfy › gson-xml
GitHub - stanfy/gson-xml: Java library for XML deserialization
November 18, 2021 - It's implemented by passing a custom JsonReader (that wraps XmlPullParsers) to Gson.
Starred by 201 users
Forked by 41 users
Languages   Java 99.5% | Shell 0.5% | Java 99.5% | Shell 0.5%
🌐
YouTube
youtube.com › jack rutorial
Convert XML file to JSON Object in Java using JAXB and Google Gson Tutorial - YouTube
Download source code and learn tutorial: http://bit.ly/2Ex6Jtw In this tutorial, we show you how to convert xml to json in Maven Project First, We will cover...
Published   February 8, 2018
Views   3K
🌐
Guru99
guru99.com › home › java tutorials › convert json to xml java using gson and jaxb with example
Convert JSON to XML Java using Gson and JAXB with Example
March 9, 2024 - In this tutorial, we briefly learned one way in which JAXB can read XML data and Gson write it to JSON.
Find elsewhere
🌐
Avoka
community.avoka.com › forums › q-a › f › q-a › 3306 › xml-to-json---google-gson-example
xml to json - Google Gson example - Q&A Forum - Q&A - Temenos Community
The Gson documentation has examples of converting primitives and objects (an instance of a class). However, I have raw xml that does not have a defining class, so the xml is not 'strictly' a Java object. Are there any resources available through Avoka documentation or other articles, or examples that I could use to get started? ... import net.sf.json.JSON import net.sf.json.xml.XMLSerializer XMLSerializer xmlSerializer = new XMLSerializer() JSON json = xmlSerializer.read(xmlString) json.toString(2)
Top answer
1 of 2
3

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

  • GSon for JSON
  • JAXB for XML

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

    }
2 of 2
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.

🌐
wissel.net
notessensei.com › blog › 2014 › 07 › from-xml-to-json-and-back.html
From XML to JSON and back - NotesSensei's Blog
July 12, 2014 - @Expose or @Expose (serialize = false, deserialize = false): You need to set GsonBuilder().excludeFieldsWithoutExposeAnnotation() to activate. Once done only fields with a Expose annotation will show up unless the annotation has the serialize/deserialize set to false Notably absent is a possibility to set a "root name". This isn't surprising, since JSON usually just starts/ends with { } and doesn't use a root name. The annotations in JAXB are much more complex, reflecting the extended possibilities of XML structure using Elements and Attributes.
🌐
GitHub
github.com › stanfy › gson-xml › blob › master › src › main › java › com › stanfy › gsonxml › GsonXml.java
gson-xml/src/main/java/com/stanfy/gsonxml/GsonXml.java at master · stanfy/gson-xml
November 18, 2021 - this.xmlParserCreator = xmlParserCreator; this.options = options; } · public Gson getGson() { return core; } · public <T> T fromXml(final String json, final Class<T> classOfT) throws JsonSyntaxException { final Object object = fromXml(json, (Type) classOfT); return Primitives.wrap(classOfT).cast(object); } ·
Author   stanfy
🌐
Baeldung
baeldung.com › home › json › jackson › convert xml to json using jackson
Convert XML to JSON Using Jackson | Baeldung
January 8, 2024 - In this tutorial, we’ll see how to convert an XML message to JSON using Jackson.
🌐
O'Reilly
oreilly.com › library › view › java-xml-and › 9781484219164 › A394211_1_En_9_Chapter.html
9. Parsing and Creating JSON Objects with Gson - Java XML and JSON [Book]
June 15, 2016 - Gson was developed with the following goals in mind: Provide simple toJson() and fromJson() methods to convert Java objects to JSON objects and ...
Author   Jeff Friesen
Published   2016
Pages   284
🌐
Avoka
community.avoka.com › forums › q-a › f › q-a › 3306 › xml-to-json---google-gson-example › 4729
xml to json - Google Gson example - Q&A Forum - Q&A - Avoka Community
I have a use case where I receive server output, from a Dynamic Data service call, in xml format and would like to convert it to JSON format. Rather than hand craft a converter I was hoping to use a library and noticed that the Google Gson library is part of the 3rd Party Java Libraries included ...
🌐
Stack Overflow
stackoverflow.com › questions › 58008569 › convert-source-xml-to-json-using-gson
java - Convert Source XML to JSON using GSON - Stack Overflow
September 19, 2019 - I am facing issue while converting the source xml into JSON using the GSON library. Please find below the code, source xml and output. I am implementing this in java mapping of SAP PI where i am ge...
🌐
ZetCode
zetcode.com › java › gson
Java Gson - JSON serialization and deserialization in Java with Gson
October 10, 2024 - Data binding API converts JSON to and from POJO using property accessors. Gson processes JSON data using data type adapters. It is similar to XML JAXB parser.
🌐
Coderanch
coderanch.com › t › 586322 › java › convert-xml-json-object-gson
How to convert xml to json object using gson (Java in General forum at Coderanch)
I would like to know whether we can convert xml to json object. Its needs to be taken care by the converter without much of change.please advice.