Java XML JAXB Marshaller marshalObjectToXml(Object object, String xmlFilePath)

Here you can find the source of marshalObjectToXml(Object object, String xmlFilePath)

Description

Creates a new XML file based on the contents and schema contained in the specified JAXB object.

License

Open Source License

Parameter

Parameter Description
object the JAXB object
xmlFilePath an absolute or relative path to the XML file

Exception

Parameter Description
JAXBException an exception

Declaration

public static <T> void marshalObjectToXml(Object object, String xmlFilePath) throws JAXBException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2011 The University of Memphis.  All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the LIDA Software Framework Non-Commercial License v1.0 
 * which accompanies this distribution, and is available at
 * http://ccrg.cs.memphis.edu/assets/papers/2010/LIDA-framework-non-commercial-v1.0.pdf
 *******************************************************************************/

import java.io.File;

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

public class Main {
    /**/*  ww  w. j a v a 2  s .  c o m*/
     * Creates a new XML file based on the contents and schema contained in the
     * specified JAXB object.
     * 
     * @param object
     *            the JAXB object
     * @param xmlFilePath
     *            an absolute or relative path to the XML file
     * @throws JAXBException
     */
    public static <T> void marshalObjectToXml(Object object, String xmlFilePath) throws JAXBException {
        if (object == null) {
            return;
        }

        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // Pretty print output
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        File xmlFile = new File(xmlFilePath);
        jaxbMarshaller.marshal(object, xmlFile);
    }
}

Related

  1. marshallToString(Object jaxbElement, Class c)
  2. marshallToXml(String zpackage, Writer writer, Object data)
  3. marshallXml(final Object object)
  4. marshallXMLInConsole(Object obj)
  5. marshalObject(Object obj, File file)
  6. marshalPackage(OutputStream printStream, final Package p)
  7. marshalToString(Object obj)
  8. marshalToString(Object obj)
  9. marshalV2(Class clazz, T obj, String uri, String nodeName)