You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matt Raible <ma...@yahoo.com> on 2002/03/06 23:12:54 UTC

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>


User Overrideable Errors

Posted by Brad Rhoads <br...@zethcon.com>.
In the action for form1 I detect several errors and warnings; the user
should be able to ignore warnings, but is required to fix errors. My
question is what's the best way to implement warnings in struts. The main
issue is how do I show the user the warnings on his first try, but move on
to the success step after the second submit of the form (assuming all the
errors have been taken care of).

In the days before struts, I would probably would have passed a flag in the
query string or as a hidden field. But it seems like this should be done by
struts.


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


Re: "Best Practice" for parsing an XML file - Code Review Requested

Posted by "David M. Karr" <dm...@earthlink.net>.
>>>>> "Matt" == Matt Raible <ma...@yahoo.com> writes:

    Matt> I've completed this task - however, it would've been MUCH easier to just use a
    Matt> properties file.  Of course, it could just be my experience with XML parsing -
    Matt> because I had to write a lot of code to grab 4 simple varaibles.

        
    Matt> 		// Get the XML Document
    Matt>         DocumentBuilderFactory builderFactory =
    Matt> DocumentBuilderFactory.newInstance();
    Matt>       	DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Matt> 		Document doc = builder.parse(is);
		
    Matt> 		// close the input stream
    Matt> 		is.close();
		
    Matt>         // get the repository root
    Matt> 		NodeList rep = doc.getElementsByTagName("root");
    Matt> 	    Node node = rep.item(0);
    Matt>         Text rootDir = (Text) node.getFirstChild();
    Matt>         config.setRepositoryRootDir(rootDir.getNodeValue());

    Matt>         // get the assets directory
    Matt>         rep = doc.getElementsByTagName("assets");
    Matt>         node = rep.item(0);
    Matt>         Text assetDir = (Text) node.getFirstChild();
    Matt>         config.setAssetDir(assetDir.getNodeValue());

You might take a look at JDOM.  It's a bit easier to use than the straight DOM
api.  The web site (<http://www.jdom.org/>) has links to several articles and
example code.

-- 
===================================================================
David M. Karr          ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net


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


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

Posted by Phase Web and Multimedia <ma...@phase.ws>.
I'll be happy to give you a working example or examine your digester code. I
would avoid JDOM. It's cool and simple but overated. I had many problems
with it working under different Tomcat installs. This was due to the xml
parser conflicts. Most of those issues, I believe, are resolved now. The
other negatives are speed and memory consumption. Digester is faster and
less consuming. These are shared observation I have had with other
independent developers that have used JDOM vs Digester.

Also, I agree with a previous poster regarding Castor. I am keeping my eyes
on it. I can't use it quite yet. But, it does sound programmer friendly and
flexible.

P.S. These are just my opinions and experiences and don't neccesarily
reflect the truth.

Brandon Goodin
Phase Web and Multimedia
P (406) 862-2245
F (406) 862-0354
mail@phase.ws
http://www.phase.ws


-----Original Message-----
From: Matt Raible [mailto:matt_raible@yahoo.com]
Sent: Thursday, March 07, 2002 10:51 AM
To: Struts Users Mailing List
Subject: RE: "Best Practice" for parsing an XML file - Code Review
Requested


I "tried" to use digester - documentation/samples weren't good enough OR I
wasn't smart enough to figure it out.

I tried for an hour or 2 to get it to work and gave up.

Matt

--- Phase Web and Multimedia <ma...@phase.ws> wrote:
> Why not use Digester. I use it to parse config info for various classes.
It
> is quite easy and fast.
>
> Brandon Goodin
> Phase Web and Multimedia
> P (406) 862-2245
> F (406) 862-0354
> mail@phase.ws
> http://www.phase.ws
>
>
> -----Original Message-----
> From: Matt Raible [mailto:matt_raible@yahoo.com]
> Sent: Wednesday, March 06, 2002 3:13 PM
> 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>
>


__________________________________________________
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>


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

Posted by Matt Raible <ma...@yahoo.com>.
I "tried" to use digester - documentation/samples weren't good enough OR I
wasn't smart enough to figure it out.

I tried for an hour or 2 to get it to work and gave up.

Matt

--- Phase Web and Multimedia <ma...@phase.ws> wrote:
> Why not use Digester. I use it to parse config info for various classes. It
> is quite easy and fast.
> 
> Brandon Goodin
> Phase Web and Multimedia
> P (406) 862-2245
> F (406) 862-0354
> mail@phase.ws
> http://www.phase.ws
> 
> 
> -----Original Message-----
> From: Matt Raible [mailto:matt_raible@yahoo.com]
> Sent: Wednesday, March 06, 2002 3:13 PM
> 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>
> 


__________________________________________________
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>


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

Posted by Phase Web and Multimedia <ma...@phase.ws>.
Why not use Digester. I use it to parse config info for various classes. It
is quite easy and fast.

Brandon Goodin
Phase Web and Multimedia
P (406) 862-2245
F (406) 862-0354
mail@phase.ws
http://www.phase.ws


-----Original Message-----
From: Matt Raible [mailto:matt_raible@yahoo.com]
Sent: Wednesday, March 06, 2002 3:13 PM
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>