parse Object To Xml File - Android XML

Android examples for XML:JAXB

Description

parse Object To Xml File

Demo Code


//package com.java2s;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {


    public static void parseToXmlFile(Object clazzToXml,
            File xmlFileToCreate) {

        JAXBContext jaxbContext;/*from  w w  w .  jav a 2 s  . c om*/

        Marshaller jaxbMarshaller;

        try {

            jaxbContext = createJaxbContext(clazzToXml);

            jaxbMarshaller = jaxbContext.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    true);

            jaxbMarshaller.marshal(clazzToXml, xmlFileToCreate);

        } catch (JAXBException e) {

            new RuntimeException(e);
        }

    }

    private static JAXBContext createJaxbContext(Object clazzToParse)
            throws JAXBException {
        return JAXBContext.newInstance(clazzToParse.getClass());
    }
}

Related Tutorials