Solved

Problem with my XML-reader only working locally

Posted on 2004-10-17
7
287 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
[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
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
JavaFX TableView not displaying correctly 3 160
jboss 7.1 start up error 1 83
swing controls 2 37
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…
Suggested Courses

734 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