Melvin
Melvin

Reputation: 258

Is There a Design Pattern to Mimic XML?

I am writing an application in Java that produces a XML file using data obtained through JDBC. This is a recursive one-to-many relationship much like the structure of an XML document. It basically looks like this:

Object A contains multiple object B's. Object B contains multiple object C's. And so on.

Is there a handy design pattern I can use for this or should I just throw a collection in each class and live with a complex DAO class?

Upvotes: 2

Views: 453

Answers (3)

OscarRyz
OscarRyz

Reputation: 199195

If the parent elements and the children elements have some common attributes you could examine the "Composite Design Pattern"

Read it and see if it suit your needs.

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245389

In OO programming land, the way to deal with a variable number of a certain type of object is to put them in a collection. Your class contains a single collection of multiple instances of Type B.

Use collections and deal with the complex DAO class.

...or if you need something more complex, you can use the Composite pattern (You'll still wind up with a collection somewhere though).

Upvotes: 4

Neel
Neel

Reputation: 854

XML is basically a tree, most tree structures will translate very well into representing XML.

Upvotes: 2

Related Questions