wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Builder to read xml? is this the right way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Builder to read xml? is this the right way?" Watch "Builder to read xml? is this the right way?" New topic
Author

Builder to read xml? is this the right way?

marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178

I created a builder - following from Joshua Block 2nd Ed Item 2 - to construct an object representation of a cell (to be rendered in say a html table through jsp later). I thought that this would be a good way to handle many optional parameters, and would readily allow for additional fields to be added to object later.

Now I am designing a class to read an xml file and to construct the cell objects.

Is there an elegant way to do this?

Currently I am thinking that I will need to use something along these line



Where if the reader does not find an attribute a null is passed to the builder. This seems a little clunky.

This is my first serious Java program, so I may be on the wrong track here.

Any help would be great.


Sample XML file snippet (having trouble rendering this)

<cell order="1> <br /> <br /> </cell> <br /> <cell order="2" width="30" style="yy">

</cell>
<cell order="3" colspan="2" value="1">

</cell>



The Builder Class

[ October 02, 2008: Message edited by: marten kay ]

when in doubt put it in parenthesis and stick a dollar sign in front of it, only good can come from this.
marten kay
Ranch Hand

Joined: Feb 03, 2007
Posts: 178

I have solved my own problem now, and thought I would let this thread know so as to close it.

Anyway, I ditched the builder pattern - even though I though it very beautiful - and instead passed a HashMap of attributes read by my XML reader to the constructor who then checked the HashMap for passed through values and if it didn't find a particular attribute in the HashMap the constructor then assigned a default value in the object.

I used a developed enum class, with a "fromString" method, to provide a level of "type" safety for all of this.

I will now move on. Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Builder to read xml? is this the right way?
 
Similar Threads
lame objects question
A setter that can only be called by framework
Private modifier problem
Cannot view data in pop-up window in ascending order
thread-safe java beans