Here you can find the source of marshalObjectToXml(Object object, String xmlFilePath)
Parameter | Description |
---|---|
object | the JAXB object |
xmlFilePath | an absolute or relative path to the XML file |
Parameter | Description |
---|---|
JAXBException | an exception |
public static <T> void marshalObjectToXml(Object object, String xmlFilePath) throws JAXBException
//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); } }