Retrieving data from XML file in Java class

Discussions

XML & Web services: Retrieving data from XML file in Java class

  1. Retrieving data from XML file in Java class (1 messages)

    Hi !
    Iam using Apache with Tomcat for Java based application and Iam new to the environment. In each application, there is an xml file "web.xml". So I edited the xml file by seeing another one to point to my database setting. But I have three questions on accessing the xml file.
    1. How to get the database info (like drivername, URL, usernmae, password) specified in the web.xml file from a JSP/Servlet file. Say the JSP file has to get a record from a table, how to retrieve the data from XML file and execute the query.
    2. How to retrieve the XML data from a normal java class (not a JSP or a servlet). Say, Iam creating an API which has methods to talk to the database and invoke the methods from JSP. If that is the case, how to retrieve the database info from the java class.
    3. Is the apache-Tomcat environment is same with weblogic 6.0 with XML stuffs.

    Thanks
    Arun J
  2. Retrieving data from XML file in Java class[ Go to top ]

    1. How to get the database info (like drivername, URL, usernmae, password) specified in the web.xml file from a JSP/Servlet file. Say the JSP file has to get a record from a table, how to retrieve the data from XML file and execute the query.
    Arun,
    First Question:
    You have many API to use in order to figure out your question such (xerces/JDOM...etc)
    I'am using xerces API for retrieval operations, hope that the following example helps you:

    public void readXMLFile(File __file)
    throws YourSpecificException
    {
    /**
     * load the xml file using the URL given in parameter
     */
    try {
    if (__file.exists()) {
    String uri = "file:" + __file.getAbsolutePath(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    document = db.parse(uri);
    } else {
    document = new DocumentImpl();
    }
    } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException("xml File - file:"+__file.getName()+"] could not be loaded");
    }
    }
    /**
     *after this you can build a class(es) that process
     * the retrieval operation
     * best to have a singleton here that retrive the
     * one by one the data associated with your know static nodes
     */
     My_Singluton inatnce = My_Singluton.getInstance();
     instance.getDriverName(document );
     instance.getpassword(document );
     instance. getuserName(document );
    .....
        }

    /**
     * your method getDrivename() will looks like
     */
     public String getDriverName() throws Your_Business_Exception
    {
     NodeList nodes = document.getChildeNodes();
     //here it's very easy to retrive any data you want
     //using the xerces API

    }

    Second Question:
    Use again xerces to build XML files, it's very easy

    Good luck,
    Said,