Convert Java bean To Xml - Java Reflection

Java examples for Reflection:Java Bean

Description

Convert Java bean To Xml

Demo Code


//package com.java2s;

import java.beans.XMLEncoder;
import java.io.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object bean = "java2s.com";
        System.out.println(beanToXml(bean));
    }/*  w ww  . j  a va 2  s .  c o  m*/

    public static String beanToXml(Object bean) {

        ByteArrayOutputStream os = new ByteArrayOutputStream(16384);
        try (XMLEncoder e = new XMLEncoder(os)) {
            e.writeObject(bean);
        }
        return os.toString();
    }
}

Related Tutorials