Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Generate XML from a arbitrary java object.

Posted on 2004-11-01
Medium Priority
Last Modified: 2013-11-21
I have like 50 Java Objects which I want to spit them as XML.

Is there a generic way to ouput a XML without having to write a special parser for each object and then writing a stream of XML.

Each of my Java object is like a TransferObject or a Value Object with set and get convention with properites as private.

Also my client who  is reveiving this XML is non-java. Infact it is a HTA container (a microsoft product for DHTML) so using the XMLSerializer by SUN is not a good option which I was exploring.

Some code will really help.


Question by:rahulkothari
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Assisted Solution

JK2429 earned 400 total points
ID: 12464893
LVL 86

Assisted Solution

CEHJ earned 400 total points
ID: 12465037

Accepted Solution

lcwiding earned 1200 total points
ID: 12465169
If the only values you are looking to place into the XML are all supported by get*() methods, you can use the reflection API to generate a simple XML structure.

int index;
Class cl = object.class;
System.out.println("<class type=\"" + cl.getName() + "\">");
Method [] methods = cl.getMethods();
for (index = 0 ; index < methods.length ; ++index)
   String name = methods[index].getName();
   Class [] params = methods[index].getParameterTypes();
   if (name.startsWith("get") && (params == null || params.length == 0))
      System.out.println("<field id=\"" + name.substring(3) + " \"  value=\"" + methods[index].invoke(object, null).toString() + "\" />");

This is a rough layout of the code, but you will want to add exception handling and output to the correct destination file.
LVL 86

Expert Comment

ID: 12465804
>>get*() methods, you can use the reflection API to generate a simple XML structure.

There's no need to reinvent the wheel in that way as that's precisely what XMLEncoder does

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

610 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