xml To Java Bean - Java Reflection

Java examples for Reflection:Java Bean

Description

xml To Java Bean

Demo Code


//package com.java2s;
import java.beans.XMLDecoder;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {


    public static Object xmlToBean(String beanXml) {

        byte[] bytes = beanXml.getBytes(StandardCharsets.UTF_8);
        return xmlDecode(bytes);
    }/*from   w  ww. j ava  2  s .  co  m*/

    public static Object xmlDecode(byte[] bytes) {

        Object obj;
        InputStream is = new ByteArrayInputStream(bytes);
        try (XMLDecoder d = new XMLDecoder(is)) {
            obj = d.readObject();
        }
        return obj;
    }

    public static <T> T xmlDecode(byte[] bytes, Class<T> clazz) {
        return (T) xmlDecode(bytes);
    }
}

Related Tutorials