Giter VIP home page Giter VIP logo

xmltojson's Introduction

XML to JSON for Android

XML to JSON is an Android Studio Library which converts easily XML to JSON and JSON to XML.

It is fully configurable so that you can change for example attribute names.

It is easy to integrate with gradle.

XML to JSON

Basic usage

There are 2 ways to create a XmlToJson object: from a String or from an InputStream.

    String xmlString;  // some XML String previously created
    XmlToJson xmlToJson = new XmlToJson.Builder(xmlString).build();

OR

    AssetManager assetManager = context.getAssets();
    InputStream inputStream = assetManager.open("myFile.xml");
    XmlToJson xmlToJson = new XmlToJson.Builder(inputStream, null).build();
    inputStream.close();

Then you can convert it to a JSONObject, a String, or a Formatted String (with indentation and line breaks).

    // convert to a JSONObject
    JSONObject jsonObject = xmlToJson.toJson();

    // convert to a Json String
    String jsonString = xmlToJson.toString();

    // convert to a formatted Json String
    String formatted = xmlToJson.toFormattedString();

Thats' it. Here is an example of XML...

<?xml version="1.0" encoding="utf-8"?>
<library>
    <owner>John Doe</owner>
    <book id="007">James Bond</book>
    <book id="000">Book for the dummies</book>
</library>

... converted into JSON

{  
   "library":{
      "owner": "John Doe",
      "book":[  
         {  
            "id":7,
            "content":"James Bond"
         },
         {  
            "id":0,
            "content":"Book for the dummies"
         }
      ]
   }
}

Custom Content names

By default, the content of a XML Tag is converted into a key called "content". This name can be changed with a custom one, using Builder.setContentName(String contentPath, String replacementName). You can change as many content names as you want.

public String convertXmlToJson(String xml) {
    XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .setContentName("/library/book", "title")
        .build();
    return xmlToJson.toString();
}
<?xml version="1.0" encoding="utf-8"?>
<library>
    <book id="007">James Bond</book>
    <book id="000">Book for the dummies</book>
</library>
{  
   "library":{  
      "book":[  
         {  
            "id":7,
            "title":"James Bond"
         },
         {  
            "id":0,
            "title":"Book for the dummies"
         }
      ]
   }
}

Custom Attributes names

Attributes are converted into key / values in the JSON. The attribute names may conflict with other keys. You can change the name of any attribute, by specifying the path to the attribute and the replacement name, using Builder.setAttributeName(String attributePath, String replacementName).

public String convertXmlToJson(String xml) {
    XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .setAttributeName("/library/book/id", "code")
        .build();
    return xmlToJson.toString();
}
<?xml version="1.0" encoding="utf-8"?>
<library>
    <book id="007">James Bond</book>
    <book id="000">Book for the dummies</book>
</library>
{  
   "library":{  
      "book":[  
         {  
            "code":7,
            "content":"James Bond"
         },
         {  
            "code":0,
            "content":"Book for the dummies"
         }
      ]
   }
}

Force a Tag to be a list

In a XML hierarchy, an entry can have children. For example, <library> has 2 entries <book>. In case there is only one book, there is no way to know that Book is a list. But you can force it using Builder.forceList(String path).

<?xml version="1.0" encoding="utf-8"?>
<library>
    <book id="007">James Bond</book>
</library>

By default, the <book> tag is NOT considered as a list

{  
   "library":{  
      "book":{  
         "id":7,
         "content":"James Bond"
      }
   }
}
public String convertXmlToJson(String xml) {
    XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .forceList("/library/book")
        .build();
    return xmlToJson.toString();
}

Now <book> is considered as a list:

{  
   "library":{  
      "book":[  
         {  
            "id":7,
            "content":"James Bond"
         }
      ]
   }
}

Force a Tag or Attribute to be a String

By default the XML attributes or content that are numbers are converted to Integer or Double. If you want to force them to be a String, then use Builder.forceStringForPath(String path).

<?xml version="1.0" encoding="utf-8"?>
<library>
    <owner>John Doe</owner>
    <book id="007">James Bond</book>
    <book id="000">Book for the dummies</book>
</library>
public String convertXmlToJson(String xml) {
    XmlToJson xmlToJson = new XmlToJson.Builder(xml)
        .Builder.forceStringForPath("/library/book/id")
        .build();
    return xmlToJson.toString();
}
{  
   "library":{
      "owner": "John Doe",
      "book":[  
         {  
            "id":"007",
            "content":"James Bond"
         },
         {  
            "id":"000",
            "content":"Book for the dummies"
         }
      ]
   }
}

Here "007" and "000" are kept as String, and not converted to 7 and 0.

Note that you can use forceStringForPath AND change the attribute or content name for the same path; the methods in the Builder can be combined. The path used in forceStringForPath is the path in the xml before eventually changing its name.

XML to JSON

Basic usage

There are several ways to create a JsonToXml object: from a Json String, a JSONObject or from an InputStream.

    JSONObject jsonObject; // some JSONObject previously created
    JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject).build();

OR

    String jsonString; // some JSON String previously created
    JsonToXml jsonToXml = new JsonToXml.Builder(jsonString).build();

OR

    AssetManager assetManager = context.getAssets();
    InputStream inputStream = assetManager.open("myFile.json");
    JsonToXml jsonToXml = new JsonToXml.Builder(inputStream).build();
    inputStream.close();

Then you can convert it to a XML String or a XML Formatted String (with indentation and line breaks)

    // Converts to a simple XML String
    String xmlString = jsonToXml.toString();

    // Converts to a formatted XML String
    int indentationSize = 3;
    String formattedXml = jsonToXml.toFormattedString(indentationSize);

Here is a JSON example

{
    "owner": {
        "id": 124,
        "name": "John Doe"
    }
}

which is converted into XML

<?xml version="1.0" encoding="UTF-8"?>
<owner>
    <id>124</id>
    <name>John Doe</name>
</owner>

Force a TAG to be an parent Attribute

You may want to use XML Attributes instead of TAG content. You can do this by using the forceAttribute method. You need to specify the Path to the TAG.

    JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject)
            .forceAttribute("/owner/id")
            .build();

The result becomes

<?xml version="1.0" encoding="UTF-8"?>
<owner id="124">
    <name>John Doe</name>
</owner>

Force a TAG to be a parent Content

When a Tag has only one child, you may want that child to be the Content for its parent. You can use the forceContent method to achieve this.

    JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject)
            .forceAttribute("/owner/id")
            .forceContent("/owner/name")
            .build();

The result becomes

<?xml version="1.0" encoding="UTF-8"?>
<owner id="124">John Doe</owner>

which is very compact :)

Installation with gradle

Add the following maven{} line to your PROJECT build.gradle file

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }		// add this line
    }
}

Add the libary dependency to your APP build.gradle file

dependencies {
    compile 'com.github.smart-fun:XmlToJson:1.2.0'    // add this line
}

License

Copyright 2016 Arnaud Guyon

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

xmltojson's People

Contributors

smart-fun avatar

Watchers

James Cloos avatar BHawk  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.