Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with my XML-reader only working locally

Posted on 2004-10-17
7
Medium Priority
?
298 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

719 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