encode Object to XML Element - Java XML

Java examples for XML:DOM Element

Description

encode Object to XML Element

Demo Code


//package com.java2s;
import java.lang.reflect.Field;
import org.w3c.dom.Element;

public class Main {
    public static void encodeObject(Object object, Element node) {
        Class classObject = object.getClass();
        Field[] fields = classObject.getFields();
        for (Field field : fields) {

            try {
                String name = field.getName();
                Object value = field.get(object);
                node.setAttribute(name, String.valueOf(value));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();/*from ww w .java 2s .co m*/
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

Related Tutorials