how to convert Java object to XML String

Hi Experts

can some help me with an example how to convert a Java Object which can have other objects as member to an XML String. Small example code will be greatly appreciated

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Using reflection you can get fields, methods and all good stuff from the class.

The args to this code are either "true" or "false"

package test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class MainClass {
	private static Boolean walkDownObjects = Boolean.FALSE;
	public static void main( String[] args ){
		if( args.length == 1 ){
			walkDownObjects = Boolean.valueOf( args[0] );	
		StringBuffer xml = new StringBuffer();		
		Class c = String.class;	
		processClass(xml, c);
		System.out.println( xml.toString() );
	public static void processClass(StringBuffer xml, Class c ){
		xml.append( "<java_class>" );		
		xml.append( "<class_name>" );
		xml.append( c.getName() );
		xml.append( "</class_name>" );
		appendFields(xml, c );
		appendMethods(xml, c );
		xml.append( "</java_class>" );
	public static void appendFields( StringBuffer xml, Class c ){
		xml.append( "<fields>" );
		Field[] fields = c.getDeclaredFields();
		for( Field f : fields ){
			xml.append( "<field>" );
			xml.append( "<type>" );
			xml.append( f.getType().getName() );
			xml.append( "</type>" );
			xml.append( "<name>" );
			xml.append( f.getName() );
			xml.append( "</name>" );
			if( !f.getType().getName().startsWith( "java" ) ){
				if( walkDownObjects ){
					processClass(xml, f.getType() );
			xml.append( "</field>" );
		xml.append( "</fields>" );
	public static void appendMethods( StringBuffer xml, Class c ){
		xml.append( "<methods>" );
		Method[] methods = c.getDeclaredMethods();
		for( Method m : methods ){
			xml.append( "<method>" );
			xml.append( "<name>" );
			xml.append( m.getName() );
			xml.append( "</name>" );
			xml.append( "<return>" );
			xml.append( m.getReturnType().getName() );
			xml.append( "</return>" );
			Class[] parameters = m.getParameterTypes();
			xml.append( "<parameters>" );
			for( Class cl : parameters ){
				xml.append( "<name>" );
				xml.append( cl.getName() );
				xml.append( "</name>" );
			xml.append( "</parameters>" );
			xml.append( "</method>" );
		xml.append( "</methods>");

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I suggest you read the some documentation about reflection.

Loads of good stuff in there.
Or, you could use Xstream.....

Doesn't it go to show the first answer is not always the best answer.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.