Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Reading XML file

  Asked By: Caleb    Date: Jul 26    Category: Java    Views: 774
  

I have an application running under Tomcat. This app
needs several XML files for the app configuration.
I'd need to access to these xml files configuration
from java class. For example, a.java needs to read
file.xml, and a.java isn't a servlet.
I have a problem with the File class, because that
needs an absolute path, and File(".") gives me the
"/bin/" jakarta path.
Is there a mechanism to load properties, paths and
other configurations like the web.xml but for the
classes of my application?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Kyle Fox     Answered On: Jul 26

Use the same classloader that loaded the classes.
Put the xml  file and other files  you wish to load  right next to the
class files, then:

InputStream is = getClass().getClassLoader().getResourceAsStream();
try
{
// use 'is' here to extract the DOM from, DocumentBuilderFactory, etc...
}
finally
{
is.close();
}

 
Didn't find what you were looking for? Find more on Reading XML file Or get search suggestion and latest updates.




Tagged: