[jdom-interest] Should I convert to/from org.jdom.Document
Naresh Bhatia
NBhatia at sapient.com
Mon Jan 21 09:53:36 PST 2002
Excellent advise! Thank you.
I just want to validate my understanding by taking this couple more levels
deep. In approach #2, say the car has an engine which has a collection of
cylinders. I am going to extend your Car class as follows:
public class Car
{
public Engine getEngine()
{
return new Engine(m_xml.getChild("engine"));
}
private Element m_xml;
}
public class Engine
{
Engine(Element xml) { m_xml = xml; }
public List getCylinders()
{
List elementList = m_xml.getChildren("cylinder");
List cylinderList = new ArrayList();
Iterator it = elementList.iterator();
while (it.hasNext()) {
Element cylinderElement = (Element)it.next();
cylinderList.add(new Cylinder(cylinderElement));
}
return cylinderList;
}
private Element m_xml;
}
To avoid recreation of the Cylinder objects every time getCyliders() is
called, one could cache cylinderList in the Engine class. However, that
would mean that cylinderList and the equivalent element list in the DOM
would have to be kept in sync. Seems to be more trouble than it is worth!
Any opinions welcome.
Thanks again,
Naresh
-----Original Message-----
From: Alex Rosen [mailto:arosen at silverstream.com]
Sent: Monday, January 21, 2002 12:10 PM
To: 'Naresh Bhatia'; jdom-interest at jdom.org
Subject: RE: [jdom-interest] Should I convert to/from org.jdom.Document
Usually you want to deal with application-specific classes for the important
concepts in your system. If you're just reading and writing a few
preferences from an XML file, then directly manipulating the XML is probably
fine. For more important or more frequently used objects, I'd build a
front-end for the JDOM objects.
You can do this in 3 different ways:
1) Your objects convert to and from XML only when necessary (e.g. on save
and load)
2) Your objects point to JDOM objects, and store their data there.
3) Your objects *are* JDOM objects (subclasses), and store their data there.
I usually recommend avoiding option #3, it leads to more hassles than it's
worth. I don't usually like option #1, because you have to remember to keep
your own representation, and the conversion logic, in sync. I like option
#2, with code along these lines:
public class Car
{
public String getManufacturer()
{
return m_xml.getAttributeValue("manufacturer");
}
public void setManufacturer(String s)
{
m_xml.setAttribute("manufacturer", s);
}
private Element m_xml;
}
-----Original Message-----
From: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]On
Behalf Of Naresh Bhatia
Sent: Friday, January 18, 2002 4:17 PM
To: 'jdom-interest at jdom.org'
Subject: [jdom-interest] Should I convert to/from org.jdom.Document
Hi,
I have a quick design question for the group. When using an XML parser such
as JDOM, is it better for the application to work directly with the document
tree (such as org.jdom.Document) or should it use an application specific
model and convert to/from the document tree? What are the pros/cons of the
two approaches, where does one make sense over the other?
Naresh Bhatia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20020121/332a5837/attachment.htm
More information about the jdom-interest
mailing list