Solved

Problem with my XML-reader only working locally

Posted on 2004-10-17
7
244 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

12 Experts available now in Live!

Get 1:1 Help Now