x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    How to convert JSON to XML in Java ?

    You can convert a JSON object to XML using JSON-Java library (json.jar), which you need to download from this link json-20190722 or using below maven dependency

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20190722</version>
    </dependency>
    

    You can convert a JSON object to XML using json library as shown below

    JSON content that you want to convert to XML

    {
        "person" {
            "firstName": "Harry",
            "lastName": "Potter",
            "addresses": [
                { "city" : "London" },
                { "city" : "New York" }
            ]
        }
    }
    	

    Expected XML

    <person>
        <firstName>Harry</firstName>
        <lastName>Potter</lastName>
        <addresses>
            <address>
                <city>London</city>
            </address>
            <address>
                <city>New York</city>
            </address>
        </addresses>
    </person>

    // Converts JSON to XML    
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.json.XML;
    	
    public class JsonToXml {
        public static void main(String[] args) throws JSONException {
    	String json = "{\"person\" :{\"firstName\": \"Harry\",\"lastName\": \"Potter\"," +
    	    "\"addresses\": [{\"city\" : \"London\" },{ \"city\" : \"New York\"}]}}";
    		
    	JSONObject jsonObject = new JSONObject(json);
            String xml = XML.toString(jsonObject);
            
    	System.out.println(xml);
        }
    }	
        

    Console Output :

    <?xml version="1.0" encoding="ISO-8859-15"?>
    <person>
        <firstName>Harry</firstName>
        <lastName>Potter</lastName>
        <addresses><city>London</city></addresses>
        <addresses><city>New York</city></addresses>
    </person>
    

    Convert XML to JSON in Java

    You can convert a XML to JSON using json library as shown below

    XML content

    <person>
        <firstName>Harry</firstName>
        <lastName>Potter</lastName>
        <addresses>
    	<address>
    	<city>London</city>
    	</address>
    	<address>
    	<city>New York</city>
    	</address>
        </addresses>
    </person>
    	

    Expected JSON

    {"person":
        {
            "firstName": "Harry",
            "lastName": "Potter",
            "addresses": 
                {"address":[
                    { "city" : "London" },
                    { "city" : "New York" }
                ]
            }
        }
    }
        
    //Converts XML to JSON
    import org.json.JSONException;
    import org.json.XML;
    	
    public class XMLToJSON {
        public static void main(String[] args) throws JSONException {
            String xml = "<person>" +
            "<firstName>Harry</firstName>" +
            "<lastName>Potter</lastName>" +
            "<addresses>" +
            "<address>" +
            "<city>London</city>" +
            "</address>" +
            "<address>" +
            "<city>New York</city>" +
            "</address>" +
            "</addresses>" +
            "</person>";
    System.out.println(XML.toJSONObject(xml).toString());
    } }

    Output :

    {"person":
    {"firstName":"Harry",
    "lastName":"Potter",
    "addresses":
    {"address":[{"city":"London"},
    {"city":"New York"}]}}}