Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Builder to read xml? is this the right way?

 
marten kay
Ranch Hand
Posts: 178
Java jQuery Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marten kay
Ranch Hand
Posts: 178
Java jQuery Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic