Recommend a design pattern to use here?

F

fred

Hi All,

I have a number of objects that I need converted to XML. In one case
I want a verbose dump to XML, all members included. In another case I
want a brief dump to XML, only some of the members included.

What would be a good pattern to use here, where I can query an {X} to
do the verbose or brief XML dump of an object for me?

Thanks,
Fred
 
A

Arne Vajhøj

I have a number of objects that I need converted to XML. In one case
I want a verbose dump to XML, all members included. In another case I
want a brief dump to XML, only some of the members included.

What would be a good pattern to use here, where I can query an {X} to
do the verbose or brief XML dump of an object for me?

I would just pass a verbosity level to the method.

Arne
 
E

Eric Sosman

Hi All,

I have a number of objects that I need converted to XML. In one case
I want a verbose dump to XML, all members included. In another case I
want a brief dump to XML, only some of the members included.

What would be a good pattern to use here, where I can query an {X} to
do the verbose or brief XML dump of an object for me?

dumpBrief(Thing thing)
dumpVerbose(Thing thing)

or

dump(Thing thing, boolean verbose)

or

dump(Thing thing, VerbosityEnum level)

or even

dump(Thing thing, Collection<ElementIdent> elements)
 
T

Tom Anderson

I have a number of objects that I need converted to XML. In one case I
want a verbose dump to XML, all members included. In another case I
want a brief dump to XML, only some of the members included.

What would be a good pattern to use here, where I can query an {X} to do
the verbose or brief XML dump of an object for me?

class Dumper {
private boolean dumpName;
private boolean dumpAddress;

public Dumper(boolean dumpName, boolean dumpAddress) {
this.dumpName = dumpName;
this.dumpAddress = dumpAddress;
}

public void dump(Customer cust, XMLWriter out) {
out.startElement("customer");
out.element("id", cust.getID()); // always dump this
if (dumpName) out.element("name", cust.getName());
if (dumpAddress) out.element("address", cust.getAddress());
out.endElement();
}
}

This is using:

http://urchin.earth.li/~twic/Code/XMLWriter.java

For the XML output, but that's not an particularly special choice.

tom
 
G

Giovanni Azua

Hello Fred,

fred said:
I have a number of objects that I need converted to XML. In one case
I want a verbose dump to XML, all members included. In another case I
want a brief dump to XML, only some of the members included.

What would be a good pattern to use here, where I can query an {X} to
do the verbose or brief XML dump of an object for me?
Sounds pretty much like you want the Visitor Pattern, though it is maybe
slightly of an overkill for this relatively small use-case of having two
Visitors only. In any case it is best be to move this non functional concern
away from the model object(s).

Possible implementation example:

// pseudo code!

class Model1 {
private String attribute1;
private int attribute2;
private double attribute3;
// ...
}

// brief - exports only few attributes
class ExportToXmlBriefVisitor extends AbstractVisitor {
public visitModel1(Model1 model) {
xml.append(model.getAttribute1());
}
}

// verbose - exports the full model
class ExportToXmlVerboseVisitor extends ExportToXmlLiteVisitor {
public visitModel1(Model1 model) {
super.visitModel1(model);
xml.append(model.getAttribute2());
xml.append(model.getAttribute3());
}
}

Visitor example http://perfectjpattern.sourceforge.net/dp-visitor.html but
note it is not the classic implementation.

Alternatively you may want to use XStream:
http://xstream.codehaus.org/tutorial.html converting to XML is a one liner
i.e. String xml = new XStream().toXml(new Model1());

then you could just use xslt to process the output xml removing different
selected attributes using the xslt "Identity Transform"
http://en.wikipedia.org/wiki/Identity_transform#Remove_Named_Element_Transform_Using_XSLT

HTH,
Best regards,
Giovanni
 
R

Roedy Green

I have a number of objects that I need converted to XML. In one case
I want a verbose dump to XML, all members included. In another case I
want a brief dump to XML, only some of the members included.

What would be a good pattern to use here, where I can query an {X} to
do the verbose or brief XML dump of an object for me?

if in a give run all the objects get converted the same way, pass it
as a parameter to the method that kicks off the batch.

If each member can be different, have it implement a method
boolean wantsBrief()

You can create a little interface for wantsBrief, or put it in some
common superclass to all your member objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top