'How to convert json data into xml in android?
I have the following Json data :-
[{"accountid":"1-RMNT","name":"NASA"},
{"accountid":"1-XQN9","name":"NewAccount"},
{"accountid":"1-Q9VF","name":"Noratel Communication"},
{"accountid":"1-RNLY","name":"Nordstrom"}]
How can I convert this JSON data to XML? Any suggestions would be appreciated.
Solution 1:[1]
I don't have a direct solution, but using gson(JSON to object) and xstream(object to XML) is doable. It may need some extra mapping code.
Solution 2:[2]
You can check the class XML.java that it´s be included in the package org.jason.
Solution 3:[3]
Do not think difficult, Its a easy way to convert XML to JSON or JSON to XML, Just open http://www.utilities-online.info/xmltojson/ and paste your json codes or xml codes. After paste and press button, then you will get Output. Its very useful for you.
Solution 4:[4]
Underscore-java has method U.jsonToXml(json).
import com.github.underscore.U;
public class Test {
public static void main(String[] args) throws Exception {
String json = "[{\"accountid\":\"1-RMNT\",\"name\":\"NASA\"},\n"
+ "{\"accountid\":\"1-XQN9\",\"name\":\"NewAccount\"},\n"
+ "{\"accountid\":\"1-Q9VF\",\"name\":\"Noratel Communication\"},\n"
+ "{\"accountid\":\"1-RNLY\",\"name\":\"Nordstrom\"}]";
System.out.println(U.jsonToXml(json));
}
}
// <?xml version="1.0" encoding="UTF-8"?>
// <root>
// <element>
// <accountid>1-RMNT</accountid>
// <name>NASA</name>
// </element>
// <element>
// <accountid>1-XQN9</accountid>
// <name>NewAccount</name>
// </element>
// <element>
// <accountid>1-Q9VF</accountid>
// <name>Noratel Communication</name>
// </element>
// <element>
// <accountid>1-RNLY</accountid>
// <name>Nordstrom</name>
// </element>
// </root>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jerry Tian |
| Solution 2 | Lukas Knuth |
| Solution 3 | It's Me SMA |
| Solution 4 |
