Java XML JAXB Marshaller marshal(Object objectToMarshal)

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

Description

Marshal the provided object using System.out

License

Apache License

Parameter

Parameter Description
objectToMarshal a parameter

Exception

Parameter Description
JAXBException an exception

Declaration

public static void marshal(Object objectToMarshal) throws JAXBException 

Method Source Code


//package com.java2s;
/*/*w  w w.  ja  va 2 s  . co  m*/
 * Copyright 2012-2013 inBloom, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    /**
     * Marshal the provided object using System.out
     *
     * @param objectToMarshal
     * @throws JAXBException
     */
    public static void marshal(Object objectToMarshal) throws JAXBException {
        marshal(objectToMarshal, System.out);
    }

    /**
     * Marshal the provided object using the OutputStream specified
     *
     * @param objectToMarshal
     * @param outputStream
     * @throws JAXBException
     */
    public static void marshal(Object objectToMarshal, OutputStream outputStream) throws JAXBException {
        if (objectToMarshal != null) {
            long startTime = System.currentTimeMillis();

            JAXBContext context = JAXBContext.newInstance(objectToMarshal.getClass());
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty("jaxb.formatted.output", Boolean.TRUE);
            marshaller.marshal(objectToMarshal, outputStream);

            System.out.println(
                    "marshaled " + objectToMarshal.getClass() + " in: " + (System.currentTimeMillis() - startTime));
        } else {
            throw new IllegalArgumentException("Cannot marshal null object");
        }
    }
}

Related

  1. marshal(Object object)
  2. marshal(Object object)
  3. marshal(Object object)
  4. marshal(Object object, File file, JAXBContext ctx)
  5. marshal(Object object, OutputStream stream)
  6. marshal(Object source, Class configurationClass)
  7. marshal(T bean, Class... bc)
  8. marshal(T clazz)
  9. marshal(T object)