• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create a java class with parame from xml using Reflection  RSS feed

 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Help.. How to create a java class with parame from xml using Reflection.

Actually I have been provided with an XML file and have been told to use Java Reflection to create a java class with parameters whose details are provided in XML file.

I am completely blank what to do?

Regards,
Rohit
[ October 11, 2007: Message edited by: Ulf Dittmer ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

"Creating a Java object from XML" is a rather unspecific description. I think it would become much clearer if you showed us what the XML looks like. You'll need to replace "<" by ""&lt;" in order for it to show up correctly.

Also note that most questions are urgent in some way, so I have removed that word from your post: EaseUp
[ October 11, 2007: Message edited by: Ulf Dittmer ]
 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

Let's consider my xml contains something like this.


Note: The parameter numbers, types may vary.

And now I want to have a java class like this using Relection at runtime.



Regards,
Rohit
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Javassist library can do this easily, assuming you know how to handle XML in Java.

Or are you looking to generate the Java source code? In that case there wouldn't be much more to it than parsing the XML and using file I/O to write the source file.
 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or are you looking to generate the Java source code? In that case there wouldn't be much more to it than parsing the XML and using file I/O to write the source file.


Yes Ulf, I need to generate the source code at runtime using properties from XMl.

And that's too using Java Reflection as being told. That's the main proprblem
I am familier with Reflection for calling methods at run time.
But not such kind of thing in which we have to read data from XML and creat Java source code as per that..

Regards,
Rohit
[ October 11, 2007: Message edited by: Rohit Menon ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection won't help with this, because it works with existing objects.

The XML FAQ links to several introductory articles about XML handling with Java.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ulf told (and as you probably know), reflection will not help you.
What i am really interested is as to why do you need such a functionality?
Who is the user that creates this file and asks you to create java file out of it?
Creating a skeleton of a class can possibly be achieved, but how do you generate method bodies?
More importantly, if someone can create this file, why cant he/she create a java file instead
It would have made sense, if it was an exisiting file type i.e a table DTD may be used to create a Data object but creating an xml file very specific to one's system and use it to generate a java class is kind of hard to digest. Of course i dont have a complete picture, so i may be missing something.
 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf and Nitesh,

It's not my requirement.
Actually I am working somewhere and it's requirement from my manager.
And he wants it because he wants it.
Moreover, he told me to use Java Reflection.

But I don't at all need methods in that class.
I just need a class with instane varibles only, with details defined in XML as I have described in code snippet in my earlier post. That's the requirement

Please if any one of you can help me.
Or let me know what I should do as I am just a beginner in this software industry.

Regards,
Rohit
[ October 11, 2007: Message edited by: Rohit Menon ]
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit Menon:
It's not my requirement.
Actually I am working somewhere and it's requirement from my manager.
And he wants it because he wants it.

Sounds like someone who doesn't know what he wants, or at least why he wants it...
Moreover, he told me to use Java Reflection.

.. who doesn't even know what he's talking about, but heard something he thought sounded cool and now wants someone else to do it for him.

But I don't at all need methods in that class.
I just need a class with instane varibles only, with details defined in XML as I have described in code snippet in my earlier post. That's the requirement

Then why the hell should some of these fields be private? Without any methods, there is no way to even access them!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please if any one of you can help me.
Or let me know what I should do as I am just a beginner in this software industry.


As I said before, you'll need to know about XML parsing, and then how to write a file. Have you read some of the links I pointed you to?
 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

I know about parsing XML using JAXP.
But after getting the parameters, do we have to use I/O stream to create a class?

But does that not seem be cumbersome??

Regards,
Rohit

[ October 12, 2007: Message edited by: Rohit Menon ]
[ October 12, 2007: Message edited by: Rohit Menon ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per what you said earlier, you're not creating the class - you're creating the Java source code file. How else but through the use of file I/O would you be able to create a file?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit Menon:
do we have to use I/O stream to create a class?
But does that not seem be cumbersome??

How would you write to a file if not through I/O stream or nio channel?
 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

Means I have to parse the XML for data
And then use I/O to create Java source code file.
There is no other alternative.
Is that the final verdict?

Regards,
Rohit
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both JAXP and file I/O are relatively simple APIs that involve just a handful of classes in this case, so I'm not sure what the big deal is about using them ... ? If you haven't used them much, this seems like a good opportunity to learn about them.
 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ulf
I will continue as per your saying only.
Thaks a lot..

Regards,
Rohit
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit Menon:

I will continue as per your saying only.

Well, since you are new to the software industry, i would say that you should not blindly follow what people say. You must do your research and find out a reason as to why someone is saying something. If you find that what you are told is not appropriate, you should question that person. Just like i think you will go down to the one who asked you to use reflection and ask the reason as to why he/she said so.
Of course, there will be people whom you trust and you can believe them, but still it always pays to do your research in the subjects you are working on. Atleast you will learn a few things.
 
Rohit Menon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nitesh

For the useful advice.
Right now I can't him directly as I am new,
but will definitely ask when the time will come..

Regards,
Rohit
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!