Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating classes

 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I accomplish the following task using reflection?
I have an xml file , part of which is shown below
<tag tagid="TAG001">
<attrib>Tag Id</attrib>
<attrib>SCV Entity</attrib>
<attrib>SCV Action</attrib>
<attrib>Change Time</attrib>
</tag>
I wanna generate classes dynamically with the elements i.e <attrib> under <tag> being the String variables and the class name being "TAG001" .The reason I say dynamically is that the xml is bound to change and have more <attrib> elements in it.I know if the xml was constant , I would use Digester.Any ideas?
jeff
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might be using the wrong term, but let's see.
You can instantiate an object (or implement a class) with reflection. However, that would require an already created class definition. I don't think that's what you want.
You can generate a class definition dynamically with byte code engineering. I think this is what you want. It means, you have no such "TAG001" class existing and you want to dynamically create/compile the class and load it into the JVM. This isn't easy, but you can use BCEL to do this:
http://jakarta.apache.org/bcel
[ January 27, 2004: Message edited by: Robert Paris ]
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to take a look at CGLIB for an easier way to do this.
 
jeff mutonho
Ranch Hand
Posts: 271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found XJR which seems to be what I'm looking for , but the documentation sux!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic