Java XML JAXB Context toXML(JAXBContext context, T obj)

Here you can find the source of toXML(JAXBContext context, T obj)

Description

Converts this response object to its XML representation unsing JAXB.

License

Open Source License

Parameter

Parameter Description
context A formerly initialized JAXB Context.
obj An object to be marshaled.

Exception

Parameter Description
JAXBException On error.

Return

XML representation of this object.

Declaration

public static <T> String toXML(JAXBContext context, T obj) throws JAXBException 

Method Source Code


//package com.java2s;
/*//from ww w  .  j  a  v  a 2  s  . com
 * Copyright (C) 2010 - present, Laszlo Csontos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import static javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT;

import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    /**
     * Converts this response object to its XML representation unsing JAXB.
     * 
     * @param context A formerly initialized JAXB Context.
     * @param obj An object to be marshaled.
     * @return XML representation of this object.
     * @throws JAXBException On error.
     */
    public static <T> String toXML(JAXBContext context, T obj) throws JAXBException {

        // Convert writer to string
        StringWriter sw = new StringWriter();

        Marshaller m = context.createMarshaller();
        m.setProperty(JAXB_FORMATTED_OUTPUT, true);
        m.marshal(obj, sw);

        return sw.toString();
    }
}

Related

  1. getRequestContext()
  2. getRightsContext()
  3. isElement(String contextPath, Object object)
  4. parseXMLFile(String fileName, String contextPath)
  5. toString(JAXBContext context, Object object)