• 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

Reading in XML into Objects

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I am working on a little XML piece and well...

I have this little XML reader method



And this is the XML file I am reading in



I want to create an instance of a House, but so far the only way I can do it is, putting a lot of little if statements like:

If (isStartElement && element.name.equals("ownerName")
set ownerName = element.getCharacter

Any suggestions?

Thanks!
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is easier way using JAXP (Java API for XML Processing). If you are already using it good.

To make you if statements go away? Why not have a House class? Can't say much without knowing architecture.
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey sorry,
I do have a house class, and it has all the properties you see in the xml document. I am having troublelinking the document to my house class.
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried Xstream?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a question for us beginners, Moving.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look into JAXB / JAX-WS, unmarshal your XML directly into the objects generated from xjc. you could wirte adaptors to wrap the generated classes to fit your needs or have constructors in your own classes that take the generated objects as args.

if don't already have a proper XSD, you can create the XSD on this site:
http://www.hitsw.com/xml_utilites/

run xjc from the command line, pass the XSD as an argument - or google how to add it as an extrnal tool in eclipse

here's some bare bones code I dug out to show how to marshal / unmarshal



rgds,
Phil
 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working wonderfully

Thanks a lot!
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic