Solved

Problem with my XML-reader only working locally

Posted on 2004-10-17
7
258 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 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

863 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

18 Experts available now in Live!

Get 1:1 Help Now