You can deal with this attribute in this way -
inputStream = XMLtoJsonConverter.class.getClassLoader().getResourceAsStream("simple.xml");
String xml = IOUtils.toString(inputStream);
System.out.println(org.json.XML.toJSONObject(xml).toString(4));
If you want to go into depth of xml to json serialization look at this. while using this, you just want to create a pojo classes of xml structure nothing more than that.
Take a look at below code -
JacksonDeserializer.java -
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class JacksonDeserializer {
private List<Item> item;
private XmlMapper xmlMapper = null;
private SimpleModule module = null;
private Channel ch = null;
public List<Item> getItem() {
return item;
}
public void setItem(List<Item> item) {
this.item = item;
}
public void readXML() throws IOException {
xmlMapper = new XmlMapper();
module = new SimpleModule();
ch = new Channel();
module.addDeserializer(List.class, ch.new ChannelDeserializer());
xmlMapper.registerModule(module);
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
URL url = new URL("some xml data available on url");
Rss rss = xmlMapper.readValue(url, Rss.class);//you can provide xml file also
Channel channel = rss.getChannel();
JacksonDeserializer obj = new JacksonDeserializer();
item = channel.getItem();
obj.setItem(item);
}
}
Rss.java -
public class Rss {
private Channel channel;
public Rss() {
}
public Channel getChannel() {
return channel;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
}
Channel.java -
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Channel {
private List<Item> item = new ArrayList();
public Channel() {
}
public List<Item> getItem() {
return item;
}
public void setItem(List<Item> item) {
this.item = item;
}
public class ChannelDeserializer extends JsonDeserializer<List<Item>> {
@Override
public List<Item> deserialize(JsonParser jp, DeserializationContext arg1) throws IOException, JsonProcessingException {
JsonNode jsonNode = jp.getCodec().readTree(jp);
String title = jsonNode.get("title").asText();
String link = jsonNode.get("link").asText();
String description = jsonNode.get("description").asText();
String pubDate = jsonNode.get("pubDate").asText();
String source = jsonNode.get("source").path("").asText();
Item i = new Item(title, link, description, pubDate, source);
item.add(i);
return item;
}
}
}
Item.java -
public class Item {
private String title;
private String link;
private String description;
private String pubDate;
private String source;
public Item() {
}
public Item(String title, String link, String description, String pubDate, String source) {
this.title = title;
this.link = link;
this.description = description;
this.pubDate = pubDate;
this.source = source;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
}
Answer from ketan on Stack OverflowConvert xml to json with Java - Stack Overflow
Quickest way to convert XML to JSON in Java - Stack Overflow
java - Convert XML to JSON format - Stack Overflow
Convert XML to Json natively?
If you are willing to learn a bit of python, this site walks you through what’s needed. I believe everything needed is distributed by RH. https://www.digitalocean.com/community/tutorials/python-xml-to-json-dict
More on reddit.comVideos
Perhaps:
{
"folders": [
{ "id":123, "private":0, "archived":0, "order":1, "title":"Shopping" },
...
]
}
Because there is not an exact correspondence between XML and JSON, you are free (e.g. have to define) how the two data-structures map. For instance, in the above, the "folder" element is implicit in the nested objects in the "folders" array.
This could be expanded as in:
"folders": [{"folder": { .... }]
Etc, but there is still the problem of not being able to capture content+attributes as consistently as XML. In any case, your data-structure -> JSON|XML serializer likely works in a particular way (and please, please, use a library, not "hand-rolled" JSON-string-munging). That is; the format of the XML and JSON should be uniformly dictated (somehow) by the data-structure for transmission.
This approach supports inverse transformation to XML:
{
"folders": {
"folder":{
"@": {
"id": "123",
"private": "0",
"archived": "0",
"order": "1"
},
"#": "Shopping"
}
}
}
It works correctly with js2xmlparser.
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
XML.toJSONObject(xml_text).toString()
org.json.XML
You can Use JSON and XML Library from json.org
import org.json.JSONObject;
import org.json.XML;
import org.junit.Test;
public class XmlToJsonTest {
private static final String XML_TEXT = "<note>\n" +
"<to>Tove</to>\n" +
"<from>Jani</from>\n" +
"<heading>Reminder</heading>\n" +
"<body>Don't forget me this weekend!</body>\n" +
"</note>";
private static final int PRETTY_PRINT_INDENT_FACTOR = 4;
@Test
public void convert() {
JSONObject xmlJSONObj = XML.toJSONObject(XML_TEXT);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
}
}
Source
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Output
{"note": {
"heading": "Reminder",
"from": "Jani",
"to": "Tove",
"body": "Don't forget me this weekend!"
}}
JSON in Java has some great resources.
Maven dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
XML.java is the class you're looking for:
import org.json.JSONObject;
import org.json.XML;
import org.json.JSONException;
public class Main {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
Output is:
{"test": {
"attrib": "moretest",
"content": "Turn this to JSON"
}}
To convert XML File in to JSON include the following dependency
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
and you can Download Jar from Maven Repository here. Then implement as:
String soapmessageString = "<xml>yourStringURLorFILE</xml>";
JSONObject soapDatainJsonObject = XML.toJSONObject(soapmessageString);
System.out.println(soapDatainJsonObject);
You may take a look at the Json-lib Java library, that provides XML-to-JSON conversion.
String xml = "<hello><test>1.2</test><test2>123</test2></hello>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read( xml );
If you need the root tag too, simply add an outer dummy tag:
String xml = "<hello><test>1.2</test><test2>123</test2></hello>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read("<x>" + xml + "</x>");
There is no direct mapping between XML and JSON; XML carries with it type information (each element has a name) as well as namespacing. Therefore, unless each JSON object has type information embedded, the conversion is going to be lossy.
But that doesn't necessarily matter. What does matter is that the consumer of the JSON knows the data contract. For example, given this XML:
<books>
<book author="Jimbo Jones" title="Bar Baz">
<summary>Foo</summary>
</book>
<book title="Don't Care" author="Fake Person">
<summary>Dummy Data</summary>
</book>
</books>
You could convert it to this:
{
"books": [
{ "author": "Jimbo Jones", "title": "Bar Baz", "summary": "Foo" },
{ "author": "Fake Person", "title": "Don't Care", "summary": "Dummy Data" },
]
}
And the consumer wouldn't need to know that each object in the books collection was a book object.
Edit:
If you have an XML Schema for the XML and are using .NET, you can generate classes from the schema using xsd.exe. Then, you could parse the source XML into objects of these classes, then use a DataContractJsonSerializer to serialize the classes as JSON.
If you don't have a schema, it will be hard getting around manually defining your JSON format yourself.