Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class Main {
    /**
     * Marshal a JAXB element to a XML DOM document
     *
     * @param jaxbElement
     *            The root of content tree to be marshalled
     * @return XML DOM document
     * @throws Exception
     *             in error case
     */
    public static Document doMarshallingJAXBObject(Object jaxbElement) throws Exception {
        if (jaxbElement == null) {
            throw new RuntimeException("No JAXB element to marshal (null)!");
        }
        Document doc = null;
        try {
            JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
            Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.newDocument();
            marshaller.marshal(jaxbElement, doc);
            doc.getDocumentElement().normalize();
        } catch (Exception e) {
            //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
            throw e;
        }

        return doc;
    }
}