XmlToJson


Source link: https://github.com/smart-fun/XmlToJson

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":"007",

 "content":"James Bond"

 
}
,

 {

"id":"000",

 "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.

<?xml version="1.0" encoding="utf-8"?> <library>
  <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)

.setContentName("/library/book", "title")

.build();

  return xmlToJson.toString();
 
}
{

"library":{

"book":[

{

"id":"007",

 "title":"James Bond"

 
}
,

 {

"id":"000",

 "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).

<?xml version="1.0" encoding="utf-8"?> <library>
  <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)

.setAttributeName("/library/book/id", "code")

.build();

  return xmlToJson.toString();
 
}
{

"library":{

"book":[

{

"code":"007",

 "content":"James Bond"

 
}
,

 {

"code":"000",

 "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":"007",

 "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":"007",

 "content":"James Bond"

 
}

 ]
 
}
 
}

Force a Tag or Attribute to be an Integer / Long / Double / Boolean

By default the XML attributes or content are processed as Strings. If you want to force them to be another type (like Integer), then use on of these methods Builder.forceIntegerForPath(String path), Builder.forceLongForPath(String path), Builder.forceDoubleForPath(String path) or Builder.forceBooleanForPath(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.forceIntegerForPath("/library/book/id")

.build();

  return xmlToJson.toString();
 
}
{

"library":{

 "owner": "John Doe",

 "book":[

{

"id":7,

 "content":"James Bond"

 
}
,

 {

"id":0,

 "content":"Book for the dummies"

 
}

 ]
 
}
 
}

Here "007" and "000" are converted to 7 and 0.

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

Skip a Tag or an Attribute

If you are not interrested in getting all the content of the XML, you can skip some Tags or some Attributes. Like for other methods you have to provide the path for the element to skip. You can use skipTag and skipAttribute as many times as you need.

<?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>
XmlToJson xmlToJson = new XmlToJson.Builder(xml)
  .skipTag("/library/owner")
  .skipAttribute("/library/book/id")
  .build();

 
{

"library":{

"book":[

{

"content":"James Bond"

 
}
,

 {

"content":"Book for the dummies"

 
}

 ]
 
}
 
}

JSON to XML

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.4.1'
 // 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.

Resources

Many ad blockers exist on Android, this is a real problem for developers that rely on ad incomes.

This project proposes an open source library that can detect most of ad blockers. Then developers can display a dialog to inform user that an ad blocker has been detected and to propose, for example, to buy an ad-free version of the application or to quit.

Chateau is a framework for adding (or improving) chat functionality in any Android app. Built in a modular way using MVP and Clean Architecture, it can easily be integrated with your chat backend with only minor changes to the included UI.

A crazy idea about bringing functional programming to Java, in an elegant way.

Android circle buttons.

A simple library for supporting self-signed certificates in Android.

JCVD is a library that helps you using the Awareness API from the Google Play Services.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes