Solved

parsing a database setting file

Posted on 2002-03-15
3
185 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

816 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

14 Experts available now in Live!

Get 1:1 Help Now