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

x
?
Solved

Generate XML from a arbitrary java object.

Posted on 2004-11-01
4
Medium Priority
?
283 Views
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.

Thanx

RK
0
Comment
Question by:rahulkothari
[X]
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
4 Comments
 
LVL 4

Assisted Solution

by:JK2429
JK2429 earned 400 total points
ID: 12464893
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 12465037
0
 
LVL 4

Accepted Solution

by:
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() + "\" />");
   }
}
System.out.println("</class>");

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

Expert Comment

by:CEHJ
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
0

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