Solved

parsing a database setting file

Posted on 2002-03-15
3
179 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
  • 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 300 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now