Java bean to XML - Java XML

Java examples for XML:XML Java Bean

Description

Java bean to XML

Demo Code


//package com.java2s;

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

import javax.xml.bind.Marshaller;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object obj = "java2s.com";
        System.out.println(bean2XML(obj));
    }//  w w  w.  j  a  v  a  2s .  com

    public static String bean2XML(Object obj) throws Exception {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller m = context.createMarshaller();
        StringWriter sw = new StringWriter();
        m.marshal(obj, sw);
        return sw.toString();
    }
}

Related Tutorials