aspose file tools*
The moose likes Java in General and the fly likes Reflecting XML into Java Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflecting XML into Java Objects" Watch "Reflecting XML into Java Objects" New topic

Reflecting XML into Java Objects

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 368
I'm wondering if there's a way to code a transfer of XML data (strings) to variables (various data types) in a java object without explicitly handling each data type. At present, I've only coded for Strings and ints, but would like to open my method for XML input to any data type, even class types created by later developers, without modifying this code. As you can see below, I'm currently handling Strings and ints explicitly, with conditionals that check data type and handle each type uniquely. I'd like to really not do that; but instead, find a way to cast to the type discovered by getType() without "if this type then that". Like, the idea is:

You'll understand the above better by looking at the code below.

I'm reading in XML data that looks like this:

And storing it in corresponding variables in a Java object.

My current code for matching the XML tag names with variable names and storing the data looks like this.

1. find the variable in the java object that matches the XML tag name.
2. check to see if the type (defined in the java object) is String or int.
3. store the value.

Correlation does not prove causality.
Tom Reilly

Joined: Jun 01, 2010
Posts: 618
Have you considered using reflection?
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 368
Tom Reilly wrote:Have you considered using reflection?

Yes, I'm in fact using reflection already. That's all the field[k] stuff. It's also used in the description I've given of what I'd like to do - although there is no cast() method where I've put it. (And it's why I title the thread: "Reflecting XML into Java Objects".)

I did go to your link though; and got another jolt of why I think there should be a way. Looking at the enhancements for version 6, it talks about further generification of getClasses() and other Class methods. I just haven't figured out how yet, and my code below is quite wrong (I think) - but it may get the general idea across. Thing is; the Class class does have a cast() method. That gives me hope.

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 368
I suspect the issue may rest on basic knowledge regarding types. The Field class has separate methods to set the value of fields with different primitive types, for example. So, at the bottom of the issue, this may come down to two basic questions.

1. Am I forced to handle primitive types explicitly (which is at least doable of course)? Because there's no way to cast, for example, a String to an int. (You have to use explicit methods.)

2. Do you see a way to handle all programmer defined classes generically; by using the Class.cast() method, for example? i.e. when field[k].getType() = "MyClass" (so to speak).
jQuery in Action, 2nd edition
subject: Reflecting XML into Java Objects