• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Build and Populate java classes at Runtime from XML file

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?


 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic