[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Generate XML from a arbitrary java object.

Posted on 2004-11-01
4
Medium Priority
?
285 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
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This installment of Make It Better gives Media Temple customers the latest news, plugins, and tutorials to make their VPS hosting experience that much smoother.
This installment of Make It Better gives Media Temple customers the latest news, plugins, and tutorials to make their Grid shared hosting experience that much smoother.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 Month11 days, 9 hours left to enroll

640 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