Load xml file

Posted on 2006-06-12
Last Modified: 2010-04-01

I have a jsp file called behind the scenes by a client html file.

The jsp page has to load an xml file which is in the same directory as the jsp page.

The only way i can get it to load is when i put the full windows path.

I use

 File thefile = new File("Thexml.xml"); //does not work

 File thefile = new File(http://correctpathtofilewhichworksinbrowser); //does not work.

File thefile = new File("../directoryabove/xmlfile.xml"); //does not work.

The only way that works is

File thefile = new File( "c:\\pathtofile");

Question by:wilflife
  • 2
  • 2
LVL 35

Assisted Solution

TimYates earned 250 total points
ID: 16883736
File theFile = new File( getServletContext().getRealPath( "/xmlfile.xml" ) ) ;


Accepted Solution

colr__ earned 250 total points
ID: 16883750
TimYates post should work, but one thing to consider - the reason you are having to put the full path in is because the relative URL is from the web server executable. For example, even though your site may be in the folder 'mySites', as far as java is concerned, your running the program from the application servers root directory, and so this location is your relative path.

Doesnt help you, but this is why your getting this result.


Author Comment

ID: 16883987
I had to use colr technique, so made sure i could map to the correct directory once i knew where i was.
I couldn't get the

new File( getServletContext().getRealPath( "/xmlfile.xml" ) ) ;

to work even when importing javax.* at the start of my jsp.

LVL 35

Expert Comment

ID: 16883994
>> I couldn't get the <snip> to work even when importing javax.* at the start of my jsp.

You shouldn't have needed to import anything...

especially not javax.* which doesn't import any classes (there is nothing inside javax.*)


Expert Comment

ID: 16883996
The only problem with that method is that it isnt portable. In most cases this wont be a problem, but good to know.


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

Suggested Solutions

With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
An analysis of the phishing scam that has been affecting Google users, along with steps to take for protection, as well as what to do if you receive one of the emails.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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

16 Experts available now in Live!

Get 1:1 Help Now