DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How to Merge Excel XLSX Files in Java
  • How to Query XML Files Using APIs in Java

Trending

  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Designing for Sustainability: The Rise of Green Software
  • Navigating Double and Triple Extortion Tactics
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  1. DZone
  2. Coding
  3. Languages
  4. Marshalling and Unmarshalling in JAXB 2.0

Marshalling and Unmarshalling in JAXB 2.0

Take a look at how to marshal and unmarshal your Java objects and XML data with JAXB 2.0, a useful tool for generating XML schemas from Java code and vice-versa.

By 
Abhishek Giri user avatar
Abhishek Giri
·
Dec. 30, 16 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
227.3K Views

Join the DZone community and get the full member experience.

Join For Free

JAXB stands for Java Architecture for XML Binding. It provides a mechanism to write Java objects into XML and read XML as objects. Simply put, you can say it is used to convert Java objects into XML and vice-versa.

JAXB provides a fast and convenient way to bind XML schemas and Java representations, making it easy for Java developers to incorporate XML data and processing functions in Java applications. As part of this process, JAXB provides methods for unmarshalling (reading) XML instance documents into Java content, and then marshalling (writing) Java content back into XML instance documents. JAXB also provides a way to generate XML schemas from Java objects.

jaxb


Features of JAXB 2.0

  • Support for all W3C XML Schema features.

  • Annotation support with the addition of the javax.xml.bind.annotation package to control this binding.

  • Additional validation capabilities through the JAXP 1.3 validation APIs.

  • Smaller runtime libraries.

Marshalling: Converting Objects to XML

In this example, we need to convert Java objects to an XML document. First, we need to create a POJO class.

import javax.xml.bind.annotation.*;

@XmlRootElement
public class Student {

    private String name;
    private int id;
    private String subject;

    Student(){
    }

    Student(String name,int id,String subject){
        this.name=name;
        this.id=id;
        this.subject=subject;
    }

    @XmlElement
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    @XmlElement
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
}


In the above class:

  • @XmlRootElement specifies the root element for the XML document.

  • @XmlAttribute specifies the attribute for the root element.

  • @XmlElement specifies the sub-element for the root element.

Now, we will call the marshaller method

try{
    //creating the JAXB context
    JAXBContext jContext = JAXBContext.newInstance(Student.class);
    //creating the marshaller object
    Marshaller marshallObj = jContext.createMarshaller();
    //setting the property to show xml format output
    marshallObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    //setting the values in POJO class
    Student student = new Student(“abhishek”, 1163, “hadoop”);
    //calling the marshall method
    marshallObj.marshal(student, new FileOutputStream(“/home/knoldus/Desktop/student.xml”));

} catch(Exception e) {
    e.printStackTrace();
}


Unmarshalling: Converting XML to Objects

try{
    //getting the xml file to read
    File file = new File(“/home/knoldus/Desktop/student.xml”);
    //creating the JAXB context
    JAXBContext jContext = JAXBContext.newInstance(Student.class);
    //creating the unmarshall object
    Unmarshaller unmarshallerObj = jContext.createUnmarshaller();
    //calling the unmarshall method
    Student student=(Student) unmarshallerObj.unmarshal(file);

    System.out.println(student.getName()+” “+student.getId()+” “+student.getSubject());
}catch(Exception e){
    e.printStackTrace();
}


XML Marshalling (computer science) Java (programming language)

Published at DZone with permission of Abhishek Giri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Thread-Safety Pitfalls in XML Processing
  • How to Merge Excel XLSX Files in Java
  • How to Query XML Files Using APIs in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!