?
Solved

how to convert Java object to XML String

Posted on 2008-02-06
4
Medium Priority
?
4,954 Views
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

Regards
-C
0
Comment
Question by:chandu33
  • 3
4 Comments
 
LVL 3

Accepted Solution

by:
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"

Enjoy
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

0
 
LVL 3

Expert Comment

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

http://java.sun.com/docs/books/tutorial/reflect/index.html


Loads of good stuff in there.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20837238
0
 
LVL 3

Expert Comment

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

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

:-)
0

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