You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by bi...@mac.com on 2003/01/04 02:29:47 UTC

Loading XML Files - Best Pracitices

I have a servlet and I want it to read it's data from an XML file.  
There's more than one way of doing this task and I'm fishing for best 
practices.
Can anyone provide me with some links to example code? I'm sure this 
has been beaten to death and I don't want to reinvent the wheel.

Thanks!


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Loading XML Files - Best Pracitices

Posted by "V. Cekvenich" <vc...@basebeans.com>.
Well....
One good practice is to read XML from a DB. Put your content in an XML 
filed in a DB table.
Then you can render many ways:
1. Stuts/Styx on Borswer Side XSLT
2. JSTL C:Transform
3. Async. by rendering to an HTML field in same table, so there is no 
view time rendering.

basiPortal.sf.net does above and... of course code is there.

.V

bido@mac.com wrote:
> I have a servlet and I want it to read it's data from an XML file.  
> There's more than one way of doing this task and I'm fishing for best 
> practices.
> Can anyone provide me with some links to example code? I'm sure this has 
> been beaten to death and I don't want to reinvent the wheel.
> 
> Thanks!




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Loading XML Files - Best Pracitices

Posted by Will Hartung <wi...@msoft.com>.
What I did was I wrote a reflective class loader based on SAX.

The idea is that with a few hints, XML files, particularly simpler XML
files, are self-describing.

Since a majority of XML fields are scalar (number, string, dates), the
loader can handle those automatically. It uses the first tag of the XML file
to locate the actual class it is loading, and then uses the reflection api
to determine the types of the assorted tags.

If the tag is a scalar, then the appropriate setter method is called,
otherwise it loads up the class and starts over.

It turned out to be ~600 lines of code, and with a few simple rules on class
design, it works great with essentially no configuration. All it needs to be
told is what class the first tag will be, and after that it figures out all
the rest.

There are no doubt other more generic XML serializers out there, but when it
is faster to write the code than it was to learn these other APIs, and the
fact that it handles 99% of the tasks we use it for, we're more than pleased
with it. I can't share it however.

After writing in we discovered that the reflection aspect was happening over
and over (when loading large collections), and that it was spectacularly
expensive. A quick cache sped it right up though.

Regards,

Will Hartung
(willh@msoft.com)




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Loading XML Files - Best Pracitices

Posted by Affan Qureshi <qu...@etilize.com>.
Depends on what you want to do with the XML data? Do you wish to load all
the data at once or do wish to implement some sort of streaming of the data.
Does it represent some comfiguration info or user data or business info?

Affan

----- Original Message -----
From: <bi...@mac.com>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Saturday, January 04, 2003 2:01 PM
Subject: Re: Loading XML Files - Best Pracitices


> I found a more than suitable solution at:
>
> www.jdom.org
>
> I'm still open for any other suggestions, though.
>
> Thanks!
> -FB
>
>
> On Friday, January 3, 2003, at 07:29  PM, bido@mac.com wrote:
>
> > I have a servlet and I want it to read it's data from an XML file.
> > There's more than one way of doing this task and I'm fishing for best
> > practices.
> > Can anyone provide me with some links to example code? I'm sure this
> > has been beaten to death and I don't want to reinvent the wheel.
> >
> > Thanks!
> >
> >
> > --
> > To unsubscribe, e-mail:
> > <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> > <ma...@jakarta.apache.org>
> >
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Loading XML Files - Best Pracitices

Posted by bi...@mac.com.
I found a more than suitable solution at:

www.jdom.org

I'm still open for any other suggestions, though.

Thanks!
-FB


On Friday, January 3, 2003, at 07:29  PM, bido@mac.com wrote:

> I have a servlet and I want it to read it's data from an XML file.  
> There's more than one way of doing this task and I'm fishing for best 
> practices.
> Can anyone provide me with some links to example code? I'm sure this 
> has been beaten to death and I don't want to reinvent the wheel.
>
> Thanks!
>
>
> --
> To unsubscribe, e-mail:   
> <ma...@jakarta.apache.org>
> For additional commands, e-mail: 
> <ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Loading XML Files - Best Pracitices

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sat, 4 Jan 2003, Jacob Kjome wrote:

>
> To tell you the truth, I would just use a SAX parser.  That will be faster
> than any of the other methods including JDOM.
>

If you've look at SAX and find it too complicated, you might want to take
a look at a couple of projects in the jakarta-commons libraries that make
dealing with XML easier.

The commons-digester package (http://jakarta.apache.org/commons/digester/)
lets you define rules that match nesting patterns for elements, plus lots
of other neat features (like automatically propogating XML attributes into
property setter calls on your beans).  This package is what Tomcat (4.1+)
itself uses to parse server.xml and web.xml files, and it's also what
packages like Struts use to parse their own configuration files.

The commons-betwixt package is designed to make it easy to go the other
direction (beans -> XML instead of XML -> beans like Digester).  It
includes some useful techniquies that map common property naming patterns
into appropriate nesting patterns of elements and attributes.  An example
app that collaborates with digester demonstrates round tripping (XML ->
bean -> XML) quite nicely.   (http://jakarta.apache.org/commons/betwixt).

Personally, I find *all* of the low-level XML processing APIs to be, um,
er, too low level :-) for day to day use.  It's much easier to use
libraries that provide a little bit higher level abstraction when that's
possible, and save the low level stuff for when it is really the only way
to go.

> Jake

Craig McClanahan


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Loading XML Files - Best Pracitices

Posted by Jacob Kjome <ho...@visi.com>.
No, I don't really have any reservations about JDOM...except for the fact 
that there has been little to zero development on it of late.  Check out 
the amount of time that has passed since the last beta release (9 - 10 
months!).  At this rate, they'll release a 1.0 version in a couple more 
years while Xerces development steams on ahead.  JDOM may be elegant, but I 
wish the main developers would put their nose to the grindstone and get, at 
least, another beta release out in short order!

To tell you the truth, I would just use a SAX parser.  That will be faster 
than any of the other methods including JDOM.

Jake

At 12:01 PM 1/4/2003 -0600, you wrote:
>Howdy,
>
>Thanks for all the good suggestions. I'm going with JDOM, just because
>it's pretty darn elegant and simple. It also appears that it will be
>(if not already) an XML standard for Java.  So all signs indicate that
>I'm not marrying myself to an obscure API.
>
>Do any of you have reservations about JDOM?
>
>Thanks,
>-FB
>
>On Saturday, January 4, 2003, at 08:02  AM, Jacob Kjome wrote:
>
>>
>>Well,
>>
>>There are a number of parsers available.  You can use DOM, JDOM,
>>DOM4J, SAX, or, actually, you might want to try out XPath using Jaxen.
>>
>>Here is an example of reading in a document using DOM....and no
>>specific external package so you don't marry yourself to a particular
>>implementation...
>>
>>DocumentBuilderFactory dbfactory =
>>DocumentBuilderFactory.newInstance();
>>dbfactory.setNamespaceAware(true);
>>Document doc = null;
>>try {
>>     DocumentBuilder dbuilder = dbfactory.newDocumentBuilder();
>>     InputStream = context.getResourceAsStream("/WEB-INF/mydoc.xml");
>>     doc = dbuilder.parse(is);
>>}
>>catch (ParserConfigurationException pce) {}
>>catch (SAXException se) {}
>>catch (IOException ioe) {}
>>
>>
>>You can then grab a NodeList of some part of the document and iterate
>>through that or you can then use Jaxen to get to specific data with
>>XPath queries....
>>
>>
>>try {
>>     XPath xpath = new
>>DOMXPath("//MyElement[@myAttribute='someSpecificValue']/ AnotherElement");
>>     Node node = (Node)xpath.selectSingleNode(domainDoc);
>>     //now do something with the node
>>}
>>catch (XPathSyntaxException xse) {}
>>catch (JaxenException je) {}
>>
>>
>>If you know, in advance, all the elements you will need to read, then
>>you might want to write a SAX parser for your document.  It will be
>>the fastest method....or you could use XML data binding using Zeus or
>>JAXB which will allow you to read in a whole document and access all
>>the data using standard Java Bean getters and set the values using
>>standard Java bean setters.  In this case, you don't even need to
>>worry about XML since the fact that it is XML is totally hidden from
>>you.  You can then marshal your updated object (assuming you modified
>>it) back to an XML document.
>>
>>There are lots of ways to do this.  Which way you choose depends on
>>your needs and what API's you feel most comfortable with.
>>
>>Jake
>>
>>
>>At 07:29 PM 1/3/2003 -0600, you wrote:
>>>I have a servlet and I want it to read it's data from an XML file.
>>>There's more than one way of doing this task and I'm fishing for best
>>>practices.
>>>Can anyone provide me with some links to example code? I'm sure this
>>>has been beaten to death and I don't want to reinvent the wheel.
>>>
>>>Thanks!
>>>
>>>
>>>--
>>>To unsubscribe, e-mail:
>>><ma...@jakarta.apache.org>
>>>For additional commands, e-mail:
>>><ma...@jakarta.apache.org>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>

Re: Loading XML Files - Best Pracitices

Posted by bi...@mac.com.
Howdy,

Thanks for all the good suggestions. I'm going with JDOM, just because  
it's pretty darn elegant and simple. It also appears that it will be  
(if not already) an XML standard for Java.  So all signs indicate that  
I'm not marrying myself to an obscure API.

Do any of you have reservations about JDOM?

Thanks,
-FB

On Saturday, January 4, 2003, at 08:02  AM, Jacob Kjome wrote:

>
> Well,
>
> There are a number of parsers available.  You can use DOM, JDOM,  
> DOM4J, SAX, or, actually, you might want to try out XPath using Jaxen.
>
> Here is an example of reading in a document using DOM....and no  
> specific external package so you don't marry yourself to a particular  
> implementation...
>
> DocumentBuilderFactory dbfactory =  
> DocumentBuilderFactory.newInstance();
> dbfactory.setNamespaceAware(true);
> Document doc = null;
> try {
>     DocumentBuilder dbuilder = dbfactory.newDocumentBuilder();
>     InputStream = context.getResourceAsStream("/WEB-INF/mydoc.xml");
>     doc = dbuilder.parse(is);
> }
> catch (ParserConfigurationException pce) {}
> catch (SAXException se) {}
> catch (IOException ioe) {}
>
>
> You can then grab a NodeList of some part of the document and iterate  
> through that or you can then use Jaxen to get to specific data with  
> XPath queries....
>
>
> try {
>     XPath xpath = new  
> DOMXPath("//MyElement[@myAttribute='someSpecificValue']/ 
> AnotherElement");
>     Node node = (Node)xpath.selectSingleNode(domainDoc);
>     //now do something with the node
> }
> catch (XPathSyntaxException xse) {}
> catch (JaxenException je) {}
>
>
> If you know, in advance, all the elements you will need to read, then  
> you might want to write a SAX parser for your document.  It will be  
> the fastest method....or you could use XML data binding using Zeus or  
> JAXB which will allow you to read in a whole document and access all  
> the data using standard Java Bean getters and set the values using  
> standard Java bean setters.  In this case, you don't even need to  
> worry about XML since the fact that it is XML is totally hidden from  
> you.  You can then marshal your updated object (assuming you modified  
> it) back to an XML document.
>
> There are lots of ways to do this.  Which way you choose depends on  
> your needs and what API's you feel most comfortable with.
>
> Jake
>
>
> At 07:29 PM 1/3/2003 -0600, you wrote:
>> I have a servlet and I want it to read it's data from an XML file.
>> There's more than one way of doing this task and I'm fishing for best  
>> practices.
>> Can anyone provide me with some links to example code? I'm sure this  
>> has been beaten to death and I don't want to reinvent the wheel.
>>
>> Thanks!
>>
>>
>> --
>> To unsubscribe, e-mail:    
>> <ma...@jakarta.apache.org>
>> For additional commands, e-mail:  
>> <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Loading XML Files - Best Pracitices

Posted by Jacob Kjome <ho...@visi.com>.
Well,

There are a number of parsers available.  You can use DOM, JDOM, DOM4J, 
SAX, or, actually, you might want to try out XPath using Jaxen.

Here is an example of reading in a document using DOM....and no specific 
external package so you don't marry yourself to a particular implementation...

DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
dbfactory.setNamespaceAware(true);
Document doc = null;
try {
     DocumentBuilder dbuilder = dbfactory.newDocumentBuilder();
     InputStream = context.getResourceAsStream("/WEB-INF/mydoc.xml");
     doc = dbuilder.parse(is);
}
catch (ParserConfigurationException pce) {}
catch (SAXException se) {}
catch (IOException ioe) {}


You can then grab a NodeList of some part of the document and iterate 
through that or you can then use Jaxen to get to specific data with XPath 
queries....


try {
     XPath xpath = new 
DOMXPath("//MyElement[@myAttribute='someSpecificValue']/AnotherElement");
     Node node = (Node)xpath.selectSingleNode(domainDoc);
     //now do something with the node
}
catch (XPathSyntaxException xse) {}
catch (JaxenException je) {}


If you know, in advance, all the elements you will need to read, then you 
might want to write a SAX parser for your document.  It will be the fastest 
method....or you could use XML data binding using Zeus or JAXB which will 
allow you to read in a whole document and access all the data using 
standard Java Bean getters and set the values using standard Java bean 
setters.  In this case, you don't even need to worry about XML since the 
fact that it is XML is totally hidden from you.  You can then marshal your 
updated object (assuming you modified it) back to an XML document.

There are lots of ways to do this.  Which way you choose depends on your 
needs and what API's you feel most comfortable with.

Jake


At 07:29 PM 1/3/2003 -0600, you wrote:
>I have a servlet and I want it to read it's data from an XML file.
>There's more than one way of doing this task and I'm fishing for best 
>practices.
>Can anyone provide me with some links to example code? I'm sure this has 
>been beaten to death and I don't want to reinvent the wheel.
>
>Thanks!
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>