- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a bean to XML using JSON-lib API in Java?
The net.sf.json.xml.XMLSerializer class is a utility class for transforming JSON to XML. When transforming JSONObject instance to XML, this class can add hints for converting back to JSON. We can use the write() method of XMLSerializer class to write a JSON value into an XML string with UTF-8 encoding and it can return a string representation of a well-formed XML document.
Syntax
public String write(JSON json)
Example
import net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLTest { public static void main(String[] args) { Student student = new Student("Sai", "Adithya", 25, "Pune"); JSONObject jsonObj = JSONObject.fromObject(student); System.out.println(jsonObj.toString(3)); //pretty print JSON XMLSerializer xmlSerializer = new XMLSerializer(); String xml = xmlSerializer.write(jsonObj); System.out.println(xml); } public static class Student { private String firstName, lastName, address; public int age; public Student(String firstName, String lastName, int age, String address) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } public String getAddress() { return address; } } }
Output
{ "firstName": "Sai", "lastName": "Adithya", "address": "Pune", "age": 25 } <?xml version="1.0" encoding="UTF-8"?> <o> <address type="string">Pune</address> <age type="number">25</age> <firstName type="string">Sai</firstName> <lastName type="string">Adithya</lastName> </o>
- Related Articles
- How to convert a bean to XML without type hints using JSON-lib API in Java?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to convert the JSON object to a bean using JSON-lib API in Java?
- How to convert a JSON array to array using JSON-lib API in Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert an array to JSON Array using JSON-lib API in Java?
- How to add elements to JSON Object using JSON-lib API in Java?
- How to convert a bean to JSON object using Exclude Filter in Java?
- How to convert XML to Json and Json back to XML using Newtonsoft.json?
- How to convert XML to JSON array in Java?
- How to convert bean to JSON object by excluding some properties using JsonConfig in Java?
- Convert a JSON object to XML format in Java?
- How to parse a JSON string using Streaming API in Java?
- How to convert JsonNode to ArrayNode using Jackson API in Java?

Advertisements