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

x
?
Solved

Problem with my XML-reader only working locally

Posted on 2004-10-17
7
Medium Priority
?
302 Views
Last Modified: 2010-03-31
Hey

I am trying to get all my vital databaseinformation from an xml-file which has the following info:

<?xml version="1.0"?>
      <jollytickerdatabaseinfo>
             <driver>com.mysql.jdbc.Driver</driver>
             <dburl>jdbc:mysql://XXX.XXX.XXX.XX/jollyticker</dburl>
            <user>masked</user>
             <password>masked</password>
      </jollytickerdatabaseinfo>

And trying to read it with:

public class ReadXML  {
void ReadXMLContent(){
 try {
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); //obtain a parser that produces DOM object trees from XML document
      DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // Defines the API to obtain DOM Document instances from an XML document
      Document doc = docBuilder.parse (new File("jolly_db.xml")); //Get the file to read

      NodeList dbUrl = doc.getElementsByTagName("dburl");  // Get node list defined by tagname, this is an list conatining all elements named dburl
      NodeList dbdriver = doc.getElementsByTagName("driver");
      NodeList dbuser = doc.getElementsByTagName("user");
      NodeList dbpassword = doc.getElementsByTagName("password");
      Element url = (Element) dbUrl.item(0); // Get the first element in the node list (we only have one element in this file)
      Element driver = (Element) dbdriver.item(0);
      Element user = (Element) dbuser.item(0);
      Element password = (Element) dbpassword.item(0);
      NodeList urlList = url.getChildNodes();  //Get child nodes
      NodeList driverList = driver.getChildNodes();
      NodeList userList = user.getChildNodes();
      NodeList passwordList = password.getChildNodes();
      strDriver = ((Node) driverList.item(0)).getNodeValue().trim();//Get the first item int the childnode (this is where our information is)
      strUrl = ((Node) urlList.item(0)).getNodeValue().trim();
      strUser =  ((Node) userList.item(0)).getNodeValue().trim();
      strPassword = ((Node) passwordList.item(0)).getNodeValue().trim();
   }
   catch (SAXParseException err) {
     System.out.println("Parsing error" + ", line " + err.getLineNumber() +", uri " + err.getSystemId());
     System.out.println(" " + err.getMessage());
   }
   catch (SAXException e) {
     Exception x = e.getException();
     ( (x == null) ? e : x).printStackTrace();
   }
   catch (Throwable t) {
     t.printStackTrace();
   }
 }
}

The problem is that when I run my applet from a website it tries to look for the xml-file on my local drive. It's looking for it on my desktop. Why? And how can I  fix this so that it looks for it on the website. All docs are in the same dir, .html, .jar, .xml

Any help would be appreciated.
0
Comment
Question by:mindweaver
  • 5
7 Comments
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 1500 total points
ID: 12333335
>>      Document doc = docBuilder.parse (new File("jolly_db.xml")); //Get the file to read


That's true. It reads local file only. The reason is u r  initialising the local file object using new File()
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12333346
rather use this one

Document       parse(String uri) ;

something like

Document doc = docBuilder.parse ("http://yoursite/yourrepository/jolly_db.xml")); //Get the file to read
0
 

Author Comment

by:mindweaver
ID: 12333350
yeah just noticed that. what shall I use instead?

like

Document doc = docBuilder.parse (jolly_db.xml);

?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12333354
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12333359
The line
      Document doc = docBuilder.parse (new File("jolly_db.xml")); //Get the file to read
loads the file from the default directory where the application starts.  The website directory needs to be passed in to your function.  It either can be a constant or can be retrieved throw the ServletContext.  In your servlet, issue the following code:
        ServletContext  context=this.getServletContext();
        String realPath=context.getRealPath("jolly_db.xml");  //Assuming xml is located on the root of the website
        ReadXML read=new ReadXML();
        read.readXMLContent(realPath);

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12333382
>>The problem is that when I run my applet from a website it tries to look for the xml-file on my local drive

According to this statement we  need to go for parse(String url) method

 

Regards
Sudhakar
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12336584
thanks for accepting.

But only 'B'??
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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