Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.