This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Need to Read XMl File From Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need to Read XMl File From Java Code" Watch "Need to Read XMl File From Java Code" New topic
Author

Need to Read XMl File From Java Code

sumeet gupta
Ranch Hand

Joined: Aug 31, 2008
Posts: 33
Hi,

I need to read an xml file from my java code inside my web application. I m putting my xml file inside WEB-INF folder of my application. I am using following code to get xml file .

InputStream is = getClass().getResourceAsStream("/" + xmlfilename);

but its not getting xmlfile from this path and giving inputstream as null. Can anybody please help me out and tell me where should I put my xml file?


Thanks,
Sumeet Gupta
Muhammad Ijaz
Ranch Hand

Joined: Mar 03, 2009
Posts: 34
try getClass().getResourceAsStream("/WEB-INF/" + xmlfilename);


IBM Certified WebSphere Commerce Application Developer

sumeet gupta
Ranch Hand

Joined: Aug 31, 2008
Posts: 33
Hi Muhammad,

Thanks for your response,I tried with this option also even though its not working.

Thanks,
Sumeet Gupta
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

getResourceAsStream uses the classloader to get the file, so any file must be in the classpath. In a web app this is anything in WEB-INF/classes or WEB-INF/lib. If your xml file were deployed in the root of the classes directory for example, your first bit of code should work.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sumeet gupta
Ranch Hand

Joined: Aug 31, 2008
Posts: 33
Thanks Paul....my error got resolved now...!!
 
 
subject: Need to Read XMl File From Java Code
 
Similar Threads
Problem with using getResourceAsStream
Placing XML on the classpath
SQL statements in files in jars - best practice suggestions?
Reading from a XML file or from Database ?