How to marshal a JAXB object into org.w3c.dom.Document

  • 23 March 2018
  • ADM

 

How to marshal a JAXB object into org.w3c.dom.Document - images/logos/java.jpg

 

Here is an example how to marshal a JAXB object into org.w3c.dom.Document

Model object

Just a random simple model object.

package com.admfactory.xml;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class User {

    private String username;
    private String email;
    private int age;
    
    public User() {
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

Example

package com.admfactory.xml;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class JAXBToDocument {
    
    public static void main(String[] args) throws Exception {
	System.out.println("JAXB Object to Document Example");
	System.out.println();
	
        // Creating the User object
        User usr = new User();
        usr.setUsername("admfactory");
        usr.setEmail("admfactory@admfactory.com");
        usr.setAge(102);
        
        // Creating the JAXBContext object
        JAXBContext jc = JAXBContext.newInstance(User.class);
        

        // Creating the Document object
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();

        // Marshaling the User object into a Document object
        Marshaller marshaller = jc.createMarshaller();      
        marshaller.marshal(usr, document);

        // writing the Document object to console
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        
        // set the properties for a formatted output - if false the output will be on one single line
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    }
}

Output

JAXB Object to Document Example

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<user>
    <age>102</age>
    <email>admfactory@admfactory.com</email>
    <username>admfactory</username>
</user>

 

References