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 Overflow
🌐
Baeldung
baeldung.com › home › json › jackson › convert xml to json using jackson
Convert XML to JSON Using Jackson | Baeldung
January 8, 2024 - >> Flexible Pub/Sub Messaging With Spring Boot and Dapr ... In this tutorial, we’ll see how to convert an XML message to JSON using Jackson.
Top answer
1 of 1
1

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;
    }
}
Discussions

java - How to convert XML data with attributes to JSON format - Stack Overflow
I am using this method to convert XML data to JSON. More on stackoverflow.com
🌐 stackoverflow.com
How would I express XML tag attributes in JSON? - Stack Overflow
There is a JSON notation / convention called badgerfish attempts to standardizes (at least its own terms) the way of preserving of most of the low level XML semantics when XML DOM represented as JSON DOM (with attributes of course) (see http://badgerfish.ning.com/). So you can easily convert back ... More on stackoverflow.com
🌐 stackoverflow.com
Quickest way to convert XML to JSON in Java - Stack Overflow
Detailed sample JAVA project can be found here: cubicrace.com/2015/06/How-to-convert-XML-to-JSON-format.html 2016-02-25T08:51:27.4Z+00:00 ... I had to copy the entire JSON package to my project and renamed the package. Adding to gradle gives warning of duplicate package from Android during build. 2016-10-09T11:04:26.34Z+00:00 ... The only problem with ... More on stackoverflow.com
🌐 stackoverflow.com
How to parse a XML file into a kotlin Data Class
🌐 r/Kotlin
14
17
February 28, 2019
🌐
Oracle
docs.oracle.com › middleware › 12213 › toplink › solutions › json.htm
16 Converting Objects to and from JSON Documents
Example 16-5 shows how to use bindings.json to map Customer and PhoneNumber classes to JSON. ... { "package-name" : "org.example", "xml-schema" : { "element-form-default" : "QUALIFIED", "namespace" : "http://www.example.com/customer" }, "java-types" : { "java-type" : [ { "name" : "Customer", "xml-type" : { "prop-order" : "firstName lastName address phoneNumbers" }, "xml-root-element" : {}, "java-attributes" : { "xml-element" : [ {"java-attribute" : "firstName","name" : "first-name"}, {"java-attribute" : "lastName", "name" : "last-name"}, {"java-attribute" : "phoneNumbers","name" : "phone-number"} ] } }, { "name" : "PhoneNumber", "java-attributes" : { "xml-attribute" : [ {"java-attribute" : "type"} ], "xml-value" : [ {"java-attribute" : "number"} ] } } ] } }
🌐
GitHub
github.com › mohapatra-sambit › xml-to-json-converter
GitHub - mohapatra-sambit/xml-to-json-converter: Java-based XML to JSON Converter – A Generic and Flexible Tool for Converting XML Data into JSON Format with Ease
Library Integration: Easily integrate the converter into Java projects as a library dependency. Schema-Based Parsing: The application parses the provided JSON schema to map XML elements and attributes into the corresponding JSON structure.
Author   mohapatra-sambit
🌐
Stack Overflow
stackoverflow.com › questions › 79563509 › how-to-convert-xml-data-with-attributes-to-json-format
java - How to convert XML data with attributes to JSON format - Stack Overflow
public JSONObject convertXMLToJson(String xml) { JSONObject json = XML.toJSONObject(xml); System.out.println(json.toString(2)); return json; } This is really effective for most XML data. But, it slightly fails when it comes to XML data with attributes.
🌐
Quora
quora.com › How-can-I-convert-XML-to-JSON-in-Java
How to convert XML to JSON in Java - Quora
Answer (1 of 3): *If you are converting this in a mmaven project add the org.json dependency to your pom.xml. ** You can add Jackson jars too your Java project to perform this conversion. Hope the above points helped you.
🌐
Javatpoint
javatpoint.com › convert-xml-to-json-in-java
Convert XML to JSON in Java - Javatpoint
Convert XML to JSON in Java - Convert XML to JSON in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
Find elsewhere
🌐
Site24x7
site24x7.com › tools › xml-to-json.html
XML to JSON Converter: Site24x7 Tools
XML attributes are converted to respective JSON keys with prefix "-".
🌐
javaspring
javaspring.net › blog › convert-xml-to-json-java
Converting XML to JSON in Java: A Comprehensive Guide — javaspring.net
The process of converting XML to JSON involves parsing the XML document, traversing its structure, and creating a corresponding JSON object. This typically requires handling elements, attributes, and text values in the XML and mapping them ...
🌐
FreeFormatter
freeformatter.com › xml-to-json-converter.html
Free Online XML to JSON Converter - FreeFormatter.com
You can add an attribute _type to your elements to infer the json type (boolean, float, integer, number, string) Terminal #text item types will be converted into a JSON property with the name #text.
🌐
GitHub
github.com › FasterXML › jackson › discussions › 140
XML to JSON with changing attribute name · FasterXML/jackson · Discussion #140
In order to get around this, we built our own JsonNodeDeserializer by extending com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer and deserialized the root element attributes into JSON at the top level. As an example: <root root_attr="1"> <child>FooBar</child> </root> BTW, We configured the XMLFactory to put '#text' in front of any text strings:
Author   FasterXML
🌐
Novixys Software
novixys.com › blog › convert-xml-json
Converting Between XML and JSON Using JAXB and Jackson | Novixys Software Dev Blog
December 12, 2017 - And here is how we convert XML to JSON. First load the XML using JAXB. Catalog catalog = JAXB.unmarshal(new File(xmlFile), Catalog.class); Next is to serialize the java object to JSON.
🌐
Stleary
stleary.github.io › JSON-java › org › json › XML.html
XML
XML uses elements, attributes, and content text, while JSON uses unordered collections of name/value pairs and arrays of values. JSON does not does not like to distinguish between elements and attributes. Sequences of similar elements are represented as JSONArrays. Content text may be placed in a "content" member. Comments, prologs, DTDs, and · &lt;[ [ ]]> are ignored. All values are converted as strings, for 1, 01, 29.0 will not be coerced to numbers but will instead be the exact value as seen in the XML document.
🌐
GitHub
github.com › lukas-krecan › json2xml
GitHub - lukas-krecan/json2xml: Java JSON to XML converter · GitHub
// convert JSON to DOM Node Node node = JsonXmlHelper.convertToDom(...); // do whatever on the DOM Node (eventually modify using XPATH it while respecting the type attributes) String json = JsonXmlHelper.convertToJson(node); This method has ...
Starred by 66 users
Forked by 28 users
Languages   Java
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-xml-to-json-array-in-java
How to convert XML to JSON array in Java?
Gson is a third-party Java library developed by Google. It is used for converting Java objects to JSON and vice versa. To know more about Gson, refer to the Gson tutorial. We can use the Gson class of the Gson library to convert XML to JSON. The Gson class is a subclass of JsonElement class.
🌐
javathinking
javathinking.com › blog › convert-xml-to-json-java
Converting XML to JSON in Java — javathinking.com
This blog post will guide you through the process of converting XML to JSON in Java, covering core concepts, typical usage scenarios, common pitfalls, and best practices. ... XML is a markup language that uses tags to define elements and their hierarchical relationships. It has a well-defined syntax with opening and closing tags, attributes, and text content.
🌐
GroupDocs Cloud
blog.groupdocs.cloud › groupdocs cloud blog › how to convert xml to json in java using rest api
How to Convert XML to JSON in Java using REST API
April 6, 2023 - How to Convert XML File into JSON in Java using REST API · GroupDocs.Conversion Cloud SDK for Java is a cloud-based document conversion solution that helps Java developers to convert various document formats to JSON in Java. It allows you to convert documents, images, spreadsheets, presentations, and many other file types to JSON with just a few lines of code.
🌐
JSON Formatter
jsonformatter.org › xml-to-json
Best XML to JSON Converter Online
XML to JSON is very unique tool for convert JOSN to XML and allows to download, save, share and print XML to JSON data.. XML to JSON support URL linking for sharing json. i.e. https://jsonformatter.org/xml-to-json/?url=https://gist.githubusercontent.com/jimmibond/8b75d0afcf249601174f1f504664072a/raw/c28e51ac26861d39f900d88e3c9fe2490374dbd9/xmlsample · No. It's not required to save and share code. If XML data is saved without login, it will become public.