Java XML JAXB Marshaller marshal(Object obj)

Here you can find the source of marshal(Object obj)

Description

marshal

License

Open Source License

Declaration

protected static DOMResult marshal(Object obj) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  .  j  a va 2s . c o m*/
 * #%L
 * Object Model Validator Core
 * %%
 * Copyright (C) 2008 - 2013 University of Toronto (baderlab.org) and Memorial Sloan-Kettering Cancer Center (cbio.mskcc.org)
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

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

import javax.xml.transform.dom.DOMResult;

public class Main {
    private static JAXBContext jaxbContext;

    protected static DOMResult marshal(Object obj) {
        DOMResult domResult = new DOMResult();
        try {
            getMarshaller().marshal(obj, domResult);
        } catch (Exception e) {
            throw new RuntimeException("Cannot serialize object: " + obj, e);
        }
        return domResult;
    }

    /**
     * Gets the results Marshaller (for validation results.)
     * @return
     */
    public static Marshaller getMarshaller() {
        //return resultsMarshaller;
        try {
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            return marshaller;
        } catch (JAXBException e) {
            throw new RuntimeException("Failed to create Marshaller", e);
        }
    }
}

Related

  1. marshal(Marshaller m, File out, Object o)
  2. marshal(Marshaller marshaller, Object object, String filename)
  3. marshal(Marshaller marshaller, Object object, String filename)
  4. marshal(Object bean)
  5. marshal(Object entity)
  6. marshal(Object obj)
  7. marshal(Object obj)
  8. marshal(Object obj, Class clazz)
  9. marshal(Object obj, OutputStream out)