[jdom-interest] converting Elements to objects

Phil Weighill-Smith phil.weighill-smith at volantis.com
Thu Feb 17 13:33:10 PST 2005


There are plenty of tools out there that will serialize (marshal) and de-serialize (unmarshal) data objects to and from XML directly. Perhaps one of those would be more appropriate to what you're trying to do. JDOM is not intended for this sort of thing.
 
If you only want to read from the XML file, you could use: http://jakarta.apache.org/commons/digester/
 
If you want to read and write the XML file, look at http://www.castor.org/. You might even like to look at http://javaalmanac.com/egs/java.beans/WriteXml.html and http://javaalmanac.com/egs/java.beans/ReadXml.html. Or http://www.wutka.com/jox.html.
 
Try googling "xml marshal", "xml java bean", "xml serialize" etc.
 
If you desparately wish to convert a JDOM document to Java Objects, you could look for a marshaller that can take a SAX event stream and use the JDOM SAXOutputter to generate the SAX event stream from your JDOM document that can feed into the marshaller.
 
Phil :n.

	-----Original Message----- 
	From: Gregory S. Hill [mailto:ghill at vmtllc.com] 
	Sent: Thu 17/02/2005 19:42 
	To: Anton Stoyanov 
	Cc: jdom-interest at jdom.org 
	Subject: Re: [jdom-interest] converting Elements to objects
	
	

	Anton,
	
	It seems to me the easiest approach would be to make the objects
	'xml-aware'.  You would need to pass each item in the list to a
	'factory' of some sort, which would identify the root element, and hand
	the contents off to an object of that class-type.  The object would then
	use JDOM to crawl through the contents, handing the sub-attributes and
	elements off to the various fields for population.
	
	Actually, if there's an easier way to do this, I'm all ears.  I've done
	this in a quick-and-dirty way in the past, but am having to prep a
	larger solution.  If a more generalized way of doing this is possible,
	I'd be interested, too.
	
	-greg
	
	Anton Stoyanov wrote:
	> Hi
	>
	> i have the following (seems common, but all the posts about that are
	> from 2000 !)
	>
	> i have an xml file like so:
	>
	> <customer>
	> <name>john</name>
	> <tel>23423423</tel>
	> etc....
	> </customer>
	>
	> and a Customer.java class which has
	>
	> String name;
	> String tel;
	> etc....
	>
	>
	> with JDOM I can get a java.util.List of elements, but how can I
	> transform them into customer objects ?
	>
	> I'm very interested in finding a solution which is independant of the
	> "customer" bit. because i have other classes too.
	>
	> thanks alot
	> if you need more information, please tell me.
	>
	>
	
	




More information about the jdom-interest mailing list