Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
/ gson2xml Public archive

Simple API to generate and XML from a JsonObject (Gson library)

License

Notifications You must be signed in to change notification settings

erickzanardo/gson2xml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

gson2xml

Simple API to generate and XML from a JsonObject (Gson library)

How to use

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("FieldInteger", 1);
        jsonObject.addProperty("FieldDouble", 1.3);
        jsonObject.addProperty("FieldString", "Test");
        jsonObject.addProperty("FieldBoolean", true);

        Gson2Xml parser = new Gson2Xml();
        String xml = parser.parse(jsonObject, "doc");

Results in:

<doc>
        <FieldInteger>1</FieldInteger>
        <FieldDouble>1.3</FieldDouble>
        <FieldString>Test</FieldString>
        <FieldBoolean>true</FieldBoolean>
</doc>

When using arrays, the default tag name for it's itens are "entry", for example, this code:

        JsonObject jsonObject = new JsonObject();
        JsonArray jsonArray = new JsonArray();
        jsonArray.add(new JsonPrimitive(1));
        jsonArray.add(new JsonPrimitive(2));
        jsonObject.add("Values", jsonArray);

        Gson2Xml parser = new Gson2Xml();
        String xml = parser.parse(jsonObject, "doc");

Results in:

<doc>
        <Values>
                <entry>1</entry>
                <entry>2</entry>
        </Values>
</doc>

You can create a map to tell gson2xml how to resolve the name of the entries of the array by passing an Map as a parameter to the Gson2Xml constructor, for example:

        JsonObject jsonObject = new JsonObject();
        JsonArray jsonArray = new JsonArray();
        jsonArray.add(new JsonPrimitive(1));
        jsonArray.add(new JsonPrimitive(2));
        jsonObject.add("Values", jsonArray);

        Map<String, String> arrayNameResolver = new HashMap<String, String>();
        arrayNameResolver.put("Values", "Value");
        Gson2Xml parser = new Gson2Xml(arrayNameResolver);
        String xml = parser.parse(jsonObject, "doc");

Resulting in:

<doc>
        <Values>
                <Value>1</Value>
                <Value>2</Value>
        </Values>
</doc>

Maven

  <repository>
    <id>erickzanardo-releases</id>
    <url>http://erickzanardo.github.com/maven/releases/</url>
  </repository>

  <dependency>
    <groupId>org.eck.json</groupId>
    <artifactId>gson2xml</artifactId>
    <version>1.0.0</version>
  </dependency>

About

Simple API to generate and XML from a JsonObject (Gson library)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages