

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 without type hints using JSON-lib API in Java?
The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can convert a bean to XML without type hints using the setTypeHintsEnabled() method of XMLSerializer class, this method sets whether JSON types can be included as attributes. We can pass false as an argument to this method to disable the type hints in XML.
Syntax
public void setTypeHintsEnabled(boolean typeHintsEnabled)
Example
import net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; public class ConvertBeanToXMLNoHintsTest { public static void main(String[] args) { Employee emp = new Employee("Krishna Vamsi", 115, 30, "Java"); JSONObject jsonObj = JSONObject.fromObject(emp); System.out.println(jsonObj.toString(3)); //pretty print JSON XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setTypeHintsEnabled(false); // this method disable type hints String xml = xmlSerializer.write(jsonObj); System.out.println(xml); } public static class Employee { private String empName, empSkill; private int empId, age; public Employee(String empName, int empId, int age, String empSkill) { super(); this.empName = empName; this.empId = empId; this.age = age; this.empSkill = empSkill; } public String getEmployeeName() { return empName; } public int getEmployeeId() { return empId; } public String getEmployeeSkill() { return empSkill; } public int getAge() { return age; } } }
Output
{ "employeeName": "Krishna Vamsi", "employeeSkill": "Java", "employeeId": 115, "age": 30 } <?xml version="1.0" encoding="UTF-8"?> <o> <age>30</age> <employeeId>115</employeeId> <employeeName>Krishna Vamsi</employeeName> <employeeSkill>Java</employeeSkill> </o>
- Related Questions & Answers
- How to convert a bean to XML 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 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 a JSON array to array 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?
- Convert a JSON object to XML format in Java?
- How to convert bean to JSON object by excluding some properties using JsonConfig in Java?
- How to parse a JSON string using Streaming API in Java?
- How to parse a JSON without duplicate keys using Gson in Java?
Advertisements