Java XML JAXB Object to XML createXML(Object o)

Here you can find the source of createXML(Object o)

Description

create XML

License

LGPL

Declaration

public static String createXML(Object o) throws JAXBException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

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

public class Main {
    public static String createXML(Object o) throws JAXBException {

        // Creating a Marshaller
        JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        StringWriter result = new StringWriter();
        jaxbMarshaller.marshal(o, result);

        // Printing XML
        String xml = result.toString();
        //System.out.println(xml);
        return xml;

    }/*from w w w  . j a  v a2 s  . c om*/
}

Related

  1. convertObjectToXml(Object object)
  2. convertToString(Object obj)
  3. convertToXml(Object obj)
  4. convertToXml(Object obj)
  5. convertToXml(Object obj, String encoding)
  6. getObjects(List> objEls)
  7. getObjects(List objEls)
  8. getXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema)
  9. getXMLString(Object person, Class clazz)