<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [jdom-interest] Should I convert to/from org.jdom.Document</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Excellent advise! Thank you.</FONT>
</P>

<P><FONT SIZE=2>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:</FONT></P>

<P><FONT SIZE=2>public class Car</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Engine getEngine()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>return new Engine(m_xml.getChild(&quot;engine&quot;));</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>private Element m_xml;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>public class Engine</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Engine(Element xml) { m_xml = xml; }</FONT>
</P>

<P><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public List getCylinders()</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List elementList = m_xml.getChildren(&quot;cylinder&quot;);</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List cylinderList = new ArrayList();</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Iterator it = elementList.iterator();</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (it.hasNext()) {</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Element cylinderElement = (Element)it.next();</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cylinderList.add(new Cylinder(cylinderElement));</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>return cylinderList;</FONT>
<BR><FONT SIZE=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>private Element m_xml;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>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!</FONT></P>

<P><FONT SIZE=2>Any opinions welcome.</FONT>
</P>

<P><FONT SIZE=2>Thanks again,</FONT>
<BR><FONT SIZE=2>Naresh</FONT>
</P>

<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: Alex Rosen [<A HREF="mailto:arosen@silverstream.com">mailto:arosen@silverstream.com</A>]</FONT>
<BR><FONT SIZE=2>Sent: Monday, January 21, 2002 12:10 PM</FONT>
<BR><FONT SIZE=2>To: 'Naresh Bhatia'; jdom-interest@jdom.org</FONT>
<BR><FONT SIZE=2>Subject: RE: [jdom-interest] Should I convert to/from org.jdom.Document</FONT>
</P>
<BR>

<P><FONT SIZE=2>Usually you want to deal with application-specific classes for the important</FONT>
<BR><FONT SIZE=2>concepts in your system. If you're just reading and writing a few</FONT>
<BR><FONT SIZE=2>preferences from an XML file, then directly manipulating the XML is probably</FONT>
<BR><FONT SIZE=2>fine. For more important or more frequently used objects, I'd build a</FONT>
<BR><FONT SIZE=2>front-end for the JDOM objects.</FONT>
</P>

<P><FONT SIZE=2>You can do this in 3 different ways:</FONT>
</P>

<P><FONT SIZE=2>1) Your objects convert to and from XML only when necessary (e.g. on save</FONT>
<BR><FONT SIZE=2>and load)</FONT>
<BR><FONT SIZE=2>2) Your objects point to JDOM objects, and store their data there.</FONT>
<BR><FONT SIZE=2>3) Your objects *are* JDOM objects (subclasses), and store their data there.</FONT>
</P>

<P><FONT SIZE=2>I usually recommend avoiding option #3, it leads to more hassles than it's</FONT>
<BR><FONT SIZE=2>worth. I don't usually like option #1, because you have to remember to keep</FONT>
<BR><FONT SIZE=2>your own representation, and the conversion logic, in sync. I like option</FONT>
<BR><FONT SIZE=2>#2, with code along these lines:</FONT>
</P>

<P><FONT SIZE=2>public class Car</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>public String getManufacturer()</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>return m_xml.getAttributeValue(&quot;manufacturer&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>}</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>public void setManufacturer(String s)</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>{</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>m_xml.setAttribute(&quot;manufacturer&quot;, s);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>}</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2>private Element m_xml;</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: jdom-interest-admin@jdom.org [<A HREF="mailto:jdom-interest-admin@jdom.org">mailto:jdom-interest-admin@jdom.org</A>]On</FONT>
<BR><FONT SIZE=2>Behalf Of Naresh Bhatia</FONT>
<BR><FONT SIZE=2>Sent: Friday, January 18, 2002 4:17 PM</FONT>
<BR><FONT SIZE=2>To: 'jdom-interest@jdom.org'</FONT>
<BR><FONT SIZE=2>Subject: [jdom-interest] Should I convert to/from org.jdom.Document</FONT>
</P>
<BR>

<P><FONT SIZE=2>Hi,</FONT>
<BR><FONT SIZE=2>I have a quick design question for the group. When using an XML parser such</FONT>
<BR><FONT SIZE=2>as JDOM, is it better for the application to work directly with the document</FONT>
<BR><FONT SIZE=2>tree (such as org.jdom.Document) or should it use an application specific</FONT>
<BR><FONT SIZE=2>model and convert to/from the document tree? What are the pros/cons of the</FONT>
<BR><FONT SIZE=2>two approaches, where does one make sense over the other?</FONT>
<BR><FONT SIZE=2>Naresh Bhatia</FONT>
</P>

</BODY>
</HTML>