You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by CARDON Denis <De...@CPR.FR> on 2002/03/07 12:17:49 UTC

RE: "Best Practice" for parsing an XML file - Code Review Request ed

Hi Matt,

I agree with Emaho, xml is (much) better for describing data. 
As for the parsing, I totally agree with you, using DOM is a pain
that one should avoid as much as possible. In your case you 
may use a more user friendly API such as Dom4j or jDom.

If you have some time to spend, you may even try to learn 
about XML schema and use Castor (castor.exolab.org) to create
a XML to bean mapping, then reading your config file will be
as easy as getting property from a bean. Example bellow : 

<config-file>
  <database-connection>
    <url>url0</url>
    <driver>driver0</driver>
    <login>login0</login>
    <password>password0</password>
  </database-connection>
</config-file>

then reading this file will be as easy as : 

//starting here!
Reader input = new FileReader("c:/configFile.xml");
ConfigFile config = ConfigFile.unmarshal(input);
DatabaseConnection db = config.getDataBaseConnection();
String url = db.getUrl();
String driver = db.getDriver();
String login = db.getLogin();
String password = db.getPassword();
//that's it!

Moreover it will tell you if your config file is not valid
(for example if you mis-spelled one element).

The only tricky part is to create the XML schema. You may 
use Xml Spy, it has a very intuitive schema editor (it has
an evaluation version). Once you have get used to it, 
doing all this should be a matter of minutes.

Hope this help

Denis


> -----Original Message-----
> From: Emaho, Ghoot [mailto:Ghoot@PETROTECHNICS.co.uk]
> Sent: Thursday, March 07, 2002 11:16 AM
> To: Struts Users Mailing List
> Subject: RE: "Best Practice" for parsing an XML file - Code Review
> Requested
> 
> 
> Matt
> 
> As an aside you may want to consider how you retrieve the 
> config xml file. Currently you are using the code
> 
> InputStream is = new FileInputStream(Constants.USER_HOME + configFile)
> 
> While this is fine, it may not always be portable, or as 
> portable as other methods. For instance Chiki has a similar 
> mechanism for loading the config xml file, but it is done using URL
> 
> conf.setConfigFile(getServletContext().getResource(chikiConfigFile))
> ...and then....
> Document doc = builder.build(getConfigFile());
> 
> This removes any hardcoding in your constants file to the 
> path of the file, and is more portable across containers. 
> 'chikiConfigFile' is  a Servlet Init parameter, so the 
> location can be changed at deploy time without changing code.
> 
> As I said, your example does work. I just wanted to point out 
> alternative method that may be more suitable. Check the Chiki 
> code for full details.
> 
> Cheers
> 
> Ghoot
> 
> 
> > -----Original Message-----
> > From: Matt Raible [mailto:matt_raible@yahoo.com]
> > Sent: 06 March 2002 22:13
> > To: Struts Users Mailing List
> > Subject: RE: "Best Practice" for parsing an XML file - Code Review
> > Requested
> > 
> > 
> > I've completed this task - however, it would've been MUCH 
> > easier to just use a
> > properties file.  Of course, it could just be my experience 
> > with XML parsing -
> > because I had to write a lot of code to grab 4 simple varaibles.
> > 
> >     private synchronized void loadConfig() throws Exception {
> > 
> >         // Initialize our configuration object
> > 		config = new Configuration();
> >         
> >         logCat.debug("Looking for " + configFile + " in " +
> > Constants.USER_HOME);
> > 		
> > 		// Acquire an input stream to our configuration file
> >         InputStream is = new 
> > FileInputStream(Constants.USER_HOME + configFile);
> > 			
> >         // No file found in user.home
> >         if (is == null) {
> >             logCat.debug("File not found at " + 
> Constants.USER_HOME +
> > configFile
> >             	+ " - looking in application's WEB-INF 
> directory");
> >             
> > 			// Look for config.xml in WEB-INF
> > 			is = getServletContext()
> >             	.getResourceAsStream("/WEB-INF/" + configFile);
> > 				
> >             if (is == null) {
> >                 throw new Exception("Configuration file '" 
> >                 	+ configFile + "' not found in '" 
> >                 	+ Constants.USER_HOME + "', nor in 
> > '/WEB-INF/'");
> >             }
> >         }
> > 		
> >         
> > 		// Get the XML Document
> >         DocumentBuilderFactory builderFactory =
> > DocumentBuilderFactory.newInstance();
> >       	DocumentBuilder builder = 
> > builderFactory.newDocumentBuilder();
> > 		Document doc = builder.parse(is);
> > 		
> > 		// close the input stream
> > 		is.close();
> > 		
> >         // get the repository root
> > 		NodeList rep = doc.getElementsByTagName("root");
> > 	    Node node = rep.item(0);
> >         Text rootDir = (Text) node.getFirstChild();
> >         config.setRepositoryRootDir(rootDir.getNodeValue());
> > 
> >         // get the assets directory
> >         rep = doc.getElementsByTagName("assets");
> >         node = rep.item(0);
> >         Text assetDir = (Text) node.getFirstChild();
> >         config.setAssetDir(assetDir.getNodeValue());
> > 
> >         // get the assetView path
> >         rep = doc.getElementsByTagName("viewPath");
> >         node = rep.item(0);
> >         Text viewPath = (Text) node.getFirstChild();
> >         config.setAssetViewPath(viewPath.getNodeValue());
> >         
> >         // get the assetView path
> >         rep = doc.getElementsByTagName("default-passing-score");
> >         node = rep.item(0);
> >         Text minScore = (Text) node.getFirstChild();
> >         config.setAssessmentMinScore(new
> > Double(minScore.getNodeValue()).doubleValue());
> >         
> >         logCat.debug(config.toString());
> >         
> >     }
> > 
> > --- Ronald Haring <Ha...@furore.com> wrote:
> > > > Nothing is wrong with the properties file...Xml is just better
> > > > 
> > > > 1.  one config.xml file in one central place...it's so much 
> > > > easier to manage
> > > > then a whole bunch of properties
> > > 
> > > You can put all your properties in one file as well, lets 
> > call that file
> > > config.properties
> > > 
> > > > 2.  xml handle the structure data much better then 
> properties file
> > > 
> > > data structure might be nice for communications between 
> > computers but for
> > > users?
> > > e.g.
> > > RepositoryRoot=d:\
> > > RepositoryAssets=assets
> > > RepositoryViewPath=file://d:/repository/assets
> > > 
> > > seems just as clear to me as
> > > > > 	<respository>
> > > > > 		<root>d:/repository</root>
> > > > > 		<assets>assets</assets>
> > > > > 		<viewPath>file://d:/repository/assets</viewPath>
> > > > > 	</respository>
> > > etc.
> > > 
> > > Cons of xml
> > > - Carefull with that ",<,> sign eugene,
> > > - Slow parsing
> > > 
> > > Gr
> > > Ronald
> > > 
> > > 
> > > Furore B.V.
> > > Rijswijkstraat 175-8
> > > Postbus 9204
> > > 1006 AE Amsterdam
> > > tel. (020) 346 71 71
> > > fax. (020) 346 71 77
> > > 
> > > 
> > --------------------------------------------------------------
> > --------------
> > > ---------------
> > > The information transmitted is intended only for the person
> > > or entity to which it is addressed and may contain confidential
> > > and/or privileged material. Any review, retransmission,
> > > dissemination or other use of, or taking of any action in
> > > reliance upon, this information by persons or entities other
> > > than the intended recipient is prohibited. If you received
> > > this in error, please contact the sender and delete the material
> > > from any computer
> > > 
> > --------------------------------------------------------------
> > --------------
> > > ---------------
> > > 
> > > 
> > 
> > 
> > __________________________________________________
> > Do You Yahoo!?
> > Try FREE Yahoo! Mail - the world's greatest free email!
> > http://mail.yahoo.com/
> > 
> > --
> > 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>