Solved

Generate XML from a arbitrary java object.

Posted on 2004-11-01
251 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
Question by:rahulkothari
    4 Comments
     
    LVL 5

    Assisted Solution

    by:JK2429
    0
     
    LVL 86

    Assisted Solution

    by:CEHJ
    0
     
    LVL 4

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    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…
    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…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now