Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading in XML into Objects

 
Ryan Waggoner
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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!
 
K. Tsang
Bartender
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried Xstream?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for us beginners, Moving.
 
phil mitchel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Working wonderfully

Thanks a lot!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic