Store xml files in resources folder (WAR), read from code


Answers

If you put the file in a directory underneath WEB-INF (or within WEB-INF itself) then you can read it using the ServletContext's getResourceAsStream method:

try {
  InputStream is = context.getResourceAsStream("/WEB-INF/file1.xml");
  ...
} catch (IOException e) {
  ...
}
Question

I have different XML-files in my 'src/main/recources' folder, and I'd like to read them from my webapplication.

File f = new File("file1.xml");
f.getAbsolutePath();

The code gets invoked inside a WebService, and this prints out 'C:\Users\Administrator' when I look inside the Tomcat-server-output. My current solution is to put the 'file1.xml'-documents outside of the WAR, in the 'C:\'-folder but this way my WAR is not transferable.

I've also tried

    <bean name="webService">
        <property name="document">
         <value>classpath:file1.xml</value>
        </property>
    </bean>

But that just prints out the "classpath:file.xml" without parsing it.

Regards, Pete




Links



Tags