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 OverflowHow would I express XML tag attributes in JSON? - Stack Overflow
java - How to convert XML data with attributes to JSON format - Stack Overflow
Quickest way to convert XML to JSON in Java - Stack Overflow
How to parse a XML file into a kotlin Data Class
Videos
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.
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);