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: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
