Solved

parsing a database setting file

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

759 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