Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smart-fun/XmlToJson
Android Library for converting XML to JSON and JSON to XML
https://github.com/smart-fun/XmlToJson
Last synced: 3 months ago
JSON representation
Android Library for converting XML to JSON and JSON to XML
- Host: GitHub
- URL: https://github.com/smart-fun/XmlToJson
- Owner: smart-fun
- License: apache-2.0
- Created: 2016-11-10T20:50:48.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-02-14T21:00:23.000Z (almost 2 years ago)
- Last Synced: 2024-08-01T21:47:25.325Z (6 months ago)
- Language: Java
- Homepage:
- Size: 253 KB
- Stars: 557
- Watchers: 12
- Forks: 112
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-android - XmlToJson - Android Library for converting XML to JSON and JSON to XML (Uncategorized / Uncategorized)
README
# 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**.
```java
String xmlString; // some XML String previously created
XmlToJson xmlToJson = new XmlToJson.Builder(xmlString).build();
```OR
```java
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).
```java
// 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
John Doe
James Bond
Book for the dummies```
... converted into JSON
```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
James Bond
Book for the dummies```
```java
public String convertXmlToJson(String xml) {
XmlToJson xmlToJson = new XmlToJson.Builder(xml)
.setContentName("/library/book", "title")
.build();
return xmlToJson.toString();
}
``````json
{
"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
James Bond
Book for the dummies```
```java
public String convertXmlToJson(String xml) {
XmlToJson xmlToJson = new XmlToJson.Builder(xml)
.setAttributeName("/library/book/id", "code")
.build();
return xmlToJson.toString();
}
``````json
{
"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, \ has 2 entries \. 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
James Bond
```
By default, the \ tag is NOT considered as a list
```json
{
"library":{
"book":{
"id":"007",
"content":"James Bond"
}
}
}
``````java
public String convertXmlToJson(String xml) {
XmlToJson xmlToJson = new XmlToJson.Builder(xml)
.forceList("/library/book")
.build();
return xmlToJson.toString();
}
```Now \ is considered as a list:
```json
{
"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
John Doe
James Bond
Book for the dummies```
```java
public String convertXmlToJson(String xml) {
XmlToJson xmlToJson = new XmlToJson.Builder(xml)
.Builder.forceIntegerForPath("/library/book/id")
.build();
return xmlToJson.toString();
}
``````json
{
"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
John Doe
James Bond
Book for the dummies```
```java
XmlToJson xmlToJson = new XmlToJson.Builder(xml)
.skipTag("/library/owner")
.skipAttribute("/library/book/id")
.build();
``````json
{
"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**.
```java
JSONObject jsonObject; // some JSONObject previously created
JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject).build();
```OR
```java
String jsonString; // some JSON String previously created
JsonToXml jsonToXml = new JsonToXml.Builder(jsonString).build();
```OR
```java
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)
```java
// 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
```json
{
"owner": {
"id": 124,
"name": "John Doe"
}
}
```which is converted into XML
```xml
124
John Doe```
### 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.
```java
JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject)
.forceAttribute("/owner/id")
.build();
```The result becomes
```xml
John Doe
```
### 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.
```java
JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject)
.forceAttribute("/owner/id")
.forceContent("/owner/name")
.build();
```The result becomes
```xml
John Doe
```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 {
implementation 'com.github.smart-fun:XmlToJson:1.5.3' // add this line
}
```## License ##
Copyright 2016-2023 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](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.