• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Populating a Java data object w/ XML data

EHobaX

Member
Hi,

I'm working on a project that requires me to populate a data object with the data within an xml file. I have a schema associated with each xml document that will be processed. The xml files will all be cookie cutter, almost the same.

My main gripe is I dont' want to have to hard code all the element tag names to associate the get and set methods to the data object along with xml element. Is there a way to associate the schema or use reflections? (someone mentioned this to me)

TIA,

E.
 
If your xml files are relatively stable and small, I would suggest hard-coding them. This route is much more efficient and cost effective.
However if the files constantly have different tags, etc... look here --> Castor Project

While I have not personally used Castor, my next door cube-mate at work is currently using it. I went with the hard-coded method...

Good luck!
 
There are a lot of tags. 🙁
That's the main reason why I wouldn't want to hard-code them.
Also if one of the tags changes (which it will no doubt will), then I'd have to go through the code and try to figure out where it's impacted. Or someone else will and then bitch at me about it.

Thanks for the link. I'll definitely look into that.

Any one else w/ suggestions??

E.
 
The Jakarta Project has a subproject called Digester that does what you want I think.
Excerpt from the main page:
Basically, the Digester package lets you configure an XML -> Java object mapping module, which triggers certain actions called rules whenever a particular pattern of nested XML elements is recognized. A rich set of predefined rules is available for your use, or you can also create your own. Advanced features of Digester include:
  • Ability to plug in your own pattern matching engine, if the standard one is not sufficient for your requirements.
  • Optional namespace-aware processing, so that you can define rules that are relevant only to a particular XML namespace.
  • Encapsulation of Rules into RuleSets that can be easily and conveniently reused in more than one application that requires the same type of processing.

EDIT: After reading your post again, it doesn't seem that Digester will work for you since you don't want to type the tags in for the rules. Sorry about that, but maybe someone else will find this info useful...
 
Back
Top