how to convert Java object to XML String

Posted on 2008-02-06
Medium Priority
Last Modified: 2010-08-24
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

Question by:chandu33
  • 3

Accepted Solution

ramiseesniblic earned 1000 total points
ID: 20835102
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


Expert Comment

ID: 20835263
I suggest you read the some documentation about reflection.


Loads of good stuff in there.
LVL 23

Expert Comment

ID: 20837238

Expert Comment

ID: 20839236
Or, you could use Xstream.....

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


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month4 days, 21 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question