• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Build and Populate java classes at Runtime from XML file

 
saakshi joshi
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am looking out for a tool or library for following requirement.
1] I have XML file e.g. something like this:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Person>
<firstname>Joe</firstname><lastname>Walnes</lastname>
<phone><code>123</code><number>1234-456</number></phone>
<fax><code>123</code><number>9999-999</number></fax>
</Person>
I don't have XSD file of this XML .

2] I don't have 'Person' java class .
Is there any tool or library available which will parse XML file and at runtime build Java class 'Person' with getter and setter for Tags like getFirstName() and setFirstName() and so on .
Similarly , it will populate the value of 'Person' class with values in xml e.g setting 'firstname' in 'Person' class as 'Joe' and so on ?


 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would your code use those classes if they didn't exist at compile time? They couldn't implement some common interface, as each XML file would have different tags, correct?

Maybe take a step back, and tell us what you're trying to achieve. For example, would this code really be expected to handle unknown XML formats? If so, what does it do?

If you did have a Person class, then you could use one of several available libraries, such as Apache Commons Digester, to populate them, or a more general API such as JAXB that can do two-way mapping.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic