Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parsing a database setting file

Posted on 2002-03-15
3
Medium Priority
?
192 Views
Last Modified: 2013-11-23
Hi!

I would like to know if there is any function or method which I can use to parse my database setting file (ie. database.xml). For example, in my web.xml file I indicated
the following:

  <servlet>
    <servlet-name>database</servlet-name>
    <servlet-class>myproj.servlet.InitServlet </servlet-class>
    <init-param>
      <param-name>database</param-name>
      <param-value>/WEB-INF/database.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

I was overriding the init method of the InitServlet class which extends HttpServlet class. because of the structure of my web.xml file, I could only get the database file which contains the setting, and this is through getInitParameter("database"), so now, how would I be able to parse the web.xml file to set the properties of the data source like setDriverClass and setUrl?

Thanks in advance!

Raymond
0
Comment
Question by:delphiusr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Expert Comment

by:saxaboo
ID: 6871035
Hi,

before thinking about parsing a XML file (which is easy), why not trying something like :

<init-param>
     <param-name>database.driver</param-name>
     <param-value>com.my.driver.ClassName</param-value>
</init-param>
<init-param>
     <param-name>database.URL</param-name>
     <param-value>myConnectString</param-value>
</init-param>
<init-param>
     <param-name>database.user</param-name>
     <param-value>scott</param-value>
</init-param>
<init-param>
     <param-name>database.password</param-name>
     <param-value>tiger</param-value>
</init-param>

Pls tell us if it helps. If you really want some XML parsing sample code, we can provide it as well.

-S
0
 

Author Comment

by:delphiusr
ID: 6872774
Hi! Thanks for the reply

I would appreciate it if you would post some XML parsing
code, this is because despite the ease of use which you provided in the above code, we were required by our project supervisor to use XML parsing. =)
0
 
LVL 3

Accepted Solution

by:
saxaboo earned 1200 total points
ID: 6873667
Sample using the SAX API :

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class MyParser extends DefaultHandler {

    private final static String SAXFACTORY = "org.apache.xerces.jaxp.SAXParserFactoryImpl";
   
   
    public void parse()
    {
        try
        {
            SAXParserFactory theFactory = (SAXParserFactory)Class.forName(SAXFACTORY).newInstance();
            SAXParser theParser = theFactory.newSAXParser();
            theParser.parse(new java.io.File("c:\\mypath\\database.xml"),this);
        }
        catch(Exception e)
        {
            //TODO : error processing
        }
    }

    public void startDocument() throws org.xml.sax.SAXException {

    }    
   
    public void endDocument() throws org.xml.sax.SAXException {
    }
   
    public void startElement(java.lang.String str, java.lang.String str1, java.lang.String str2, org.xml.sax.Attributes attributes) throws org.xml.sax.SAXException {
    }
   
    public void endElement(java.lang.String str, java.lang.String str1, java.lang.String str2) throws org.xml.sax.SAXException {
    }
   
}

To run this sample, you need a SAX XML parser. Here I used the org.apache.xerces.jaxp.SAXParserFactoryImpl class, which means you need to download the Xerces parser from http://xml.apache.org and put xerces.jar in your classpath.

What will happen is that when you call parse(), the XML file will be read, and for each element (e.g. <myelt tot="tata">foo</myelt>), the callbacks startElement() and endElement() will be called, and provided with the set of attributes.

So your job now is to put your code extracting the parameters from your file in startElement().

Re-post if you have any problems, and give me the format of your databse.xml file. Pls note that the documentation for the SAX API is available in the JDK docs.

Hope this helps,

-S
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question