File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cant read XML file

 
Pappu Kumar
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shankar vembu
Ranch Hand
Posts: 309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.getResource() searches for resources in the classpath. It uses the the classloader using which ur class was loaded to do this. So if ur resource is reachable by the classloader meaning if it is in the classpath, then there should be no problem. How do you know that it is searching for the resource in the tomcat folder??
Regards
 
shankar vembu
Ranch Hand
Posts: 309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and more way to read resources would be to use ServletContext.getResourceAsStream(path). This will search in your web context directory.
regards.
 
Pappu Kumar
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i know its searching there ??
because it says...cant find
c:\Program files\Apache Group\Tomcat 4.1\Delegator.xml
and i am not reading through a servlet , a Utility class is reading it
 
Pappu Kumar
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i meant
c:\Program files\Apache Group\Tomcat 4.1\Trial.xml
 
shankar vembu
Ranch Hand
Posts: 309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pappu Kumar:
i meant
c:\Program files\Apache Group\Tomcat 4.1\Trial.xml

from the javadocs for Class.getResourceAsStream()
Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.
So this method would return null, if the resource is not found. Thats the resaon I asked how du know that the resource is being searched in your tomcat folder. I think your utility class is misleading you with the message you are getting. Try to run the program without the utility class.
Regards.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic