In this blog we will discuss Java Object to xml.JAXB marshalling - convert java object to xml. Also we will check how to convert Java objet to xml file.
Java object to xml | JAXB marshalling - convert java object to xml | c
Maven Dependency:
<!-- https://mvnrepository.com/artifact/javax.xml/jaxb-api -->
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.0</version>
</dependency>
Spring Boot Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
Program for Converting Java Object to xml String:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ConvertJavaObjectToXml {
public static void main(String[] args) {
Person person= new Person("Sunil",24,"India");
try {
JAXBContext contextObj = JAXBContext.newInstance(Person.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// marshallerObj.marshal(que, new FileOutputStream("question.xml"));
StringWriter sw = new StringWriter();
marshallerObj.marshal(person,sw);
String xmlString=sw.toString();
System.out.println("xmlString "+xmlString);
marshallerObj.marshal(person, new FileOutputStream("person.xml"));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
package com.mkyong;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Person {
private String name;
//@XmlElement
private int age;
//@XmlElement
private String address;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
@XmlElement
public void setAddress(String address) {
this.address = address;
}
}
Program for Converting Java Object to Xml File
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class ConvertJavaObjectToXml {
public static void main(String[] args) {
Person person= new Person("Sunil",24,"India");
try {
JAXBContext contextObj = JAXBContext.newInstance(Person.class);
Marshaller marshallerObj = contextObj.createMarshaller();
//This line is used to format xml file
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshallerObj.marshal(person,sw);
marshallerObj.marshal(person, new FileOutputStream("person.xml"));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Note: your xml willbe stored in same location where your project existing.
No comments:
Post a Comment