I’d like to take a xml file from my classpath to unmarshal it and use it for testing purposes.
My problem is to get it as an InputStream.
I wrote these lines but I always get a null result.
InputStream is = getClass().getResourceAsStream("WebContent/WEB-INF/classes/testing/"+ COMPLETE_DOCUMENT + ".xml");
of course the path you see in the method argument is the one to my file.
I tried several combinations:
WebContent/WEB-INF/classes/testing/
classpath:testing/
classpath*:testing/
but I always get the InputStream = null.
I even tried to switch to
ClassLoader.getResourceAsStream(...)
but nothing happens. I suppose the path to the resource is somehow wrong, but I can’t figure out where. From my servlet.xml I use some resource in the classpath configuring PropertyPlaceholderConfigurer or Jaxb2Marshaller just with the syntax
"classpath:folder/file.xsd"
and it works perfectly. The folder I want to load my xml from is a sibling of the one in the example above.
What am I missing?
EDIT :
I try to follow the spring ClassPathResource helper class approach and I get a strange behaviour: as I said before I already have some resources loaded from the classpath by some spring beans at the startup. If I use the path to such resources in the code suggested by dardo in as follows:
ClassPathResource cpr = new ClassPathResource("xmlschemas/lrinode.xsd");
InputStream is = cpr.getInputStream();
I Still get a FileNotFound Exception!
Of course "xmlschemas/lrinode.xsd" is a xsd I load at the startup successfully. It doesn’t work even if I use the full path to the resource, starting from the root of the application.
I’m starting to think I’m missing something trivial.
Spring provides a helper class named ClassPathResource
So something like:
Should work, hope this helps!
Link to API Doc:
http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/core/io/ClassPathResource.html
Sidenote
Also, if you’re using it for testing purposes, might want to wire a bean mapped to the xsd.
Might want to look into a JAXB marshaller
http://static.springsource.org/spring-ws/site/reference/html/oxm.html#oxm-jaxb2-xsd