Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parsing a database setting file

Posted on 2002-03-15
3
Medium Priority
?
193 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

963 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