Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1733
  • Last Modified:

Cant read XML file

I have a XML file which i put in the WEB-INF/CLASSES folder , but when i run my code that tries to read this xml file , Here is the snippet of the code.
************************************************
Document doc = XMLUtil.getDocument(new InputSource((new Object()).getClass().getResourceAsStream("/" + "Trial.xml")));
*************************************************
, it always looks for the file in my c:\program files\apache group\Tomcat 4.1 directory.....ultimately i will be running all code on UNIX and i wont have access to directories outside my web-app , so how do i solve this issue ?? Please help
0
rahulkothari
Asked:
rahulkothari
1 Solution
 
TimYatesCommented:
Can't you do:

Document doc = XMLUtil.getDocument(new InputSource( getClass().getResourceAsStream( "/" + "Trial.xml" ) ) ) ;
0
 
rahulkothariAuthor Commented:
Nope doesnt work.....still looks in the same place
0
 
TimYatesCommented:
hmmm...

what about if you put it in the same directory as the class you are in, then do something like:

Document doc = XMLUtil.getDocument( new InputSource( getClass().getResourceAsStream( "Trial.xml" ) ) ) ;

?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CEHJCommented:
>>Nope doesnt work.....still looks in the same place

How do you know where it's looking?
0
 
amit_chauhanCommented:
As TimYates mentioned, remove the "/", just use the file name.

Thanks
Amit
0
 
anthony_castillonCommented:
I guess putting it in the same directory as your classes is a simple way to do it.  I'd probably create a new folder under the WEB-INF folder (e.g. 'misc').  In the code, I'll call it this way:

Document doc = XMLUtil.getDocument( new InputSource( getClass().getResourceAsStream( "/misc/Trial.xml" ) ) ) ;
0
 
anthony_castillonCommented:
Correction:  It should have read:
Document doc = XMLUtil.getDocument( new InputSource( getClass().getResourceAsStream( "/WEB-INF/misc/Trial.xml" ) ) ) ;

The reason I'll create the misc folder is that I don't wanna mix up my .class files with other files.

0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now