Jump to content

Get Generic Object from XML in Java


proggR

Recommended Posts

I'm trying to make a generic class that receives an object of any type and some xml and can map the XML to the object based on a query that is either also passed or is built into the object. Its easy enough to do it when you know what object will be passed in but I want the method to be able to be used in a number of situations.

 

My class is XMLParser<T> and the method is public T getObjectFromXML(String xml, String query). When I instantiate XMLParser I also pass in a Class<T> object to give access to information about the class that T is representing.

 

That's as far as I've got. I've toyed with getting a NodeList from the XML and getting a Field[] from the Class<T> object but I can't figure out how to assign any values to the fields in the instantiated T object.

 

I've found some things that are made to do exactly what I'm doing (Castor was the first one I found) but I've been trying to get it working for so long now that I'd really like to write it myself.

 

Any help would be great. If worse comes to worse I can code the methods for the concrete classes that I know I'll need but I was really hoping to make it work with Generics.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/228301-get-generic-object-from-xml-in-java/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.