Generate objects from XML?

J

Jimbo

I've never used XML and Java together, so apologies for the basic question.
In fact I've hardly ever used XML before to be honest.

How do I extract data from an XML file and use that data to generate an
object? Obviously I'll generate the XML elsewhere, and define the classes
myself - but I want to instantiate the classes by reading from an XML data
source and filling in the attributes of the class using the data from the
XML.

Any tips, techniques, advice or pointers to reading material would be
appreciated.
 
W

Wendy Smoak

Jimbo said:
How do I extract data from an XML file and use that data to generate an
object? Obviously I'll generate the XML elsewhere, and define the classes
myself - but I want to instantiate the classes by reading from an XML data
source and filling in the attributes of the class using the data from the
XML.

I use Jakarta Commons Digester for this. It's a layer on top of a SAX
parser, and you set up rules to create objects and call methods when certain
tags are encountered.

http://jakarta.apache.org/commons/digester/
 
I

iksrazal

Jimbo said:
I've never used XML and Java together, so apologies for the basic question.
In fact I've hardly ever used XML before to be honest.

How do I extract data from an XML file and use that data to generate an
object? Obviously I'll generate the XML elsewhere, and define the classes
myself - but I want to instantiate the classes by reading from an XML data
source and filling in the attributes of the class using the data from the
XML.

Any tips, techniques, advice or pointers to reading material would be
appreciated.

I recommend using JAXB for this, but you need an XML Schema.
Basically, JAXB will convert the Schema to Java source files. Then you
can create XML objects from Java and vice-versa.

Take a look at this article for an intro.

http://www.onjava.com/pub/a/onjava/2004/05/05/jaxb.html?page=1

HTH,
iksrazal
http://www.braziloutsource.com/
 

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
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top