Note that there are some explanatory texts on larger screens.

plurals
  1. POload a (xml) file from the classpath in a spring web app
    primarykey
    data
    text
    <p>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.</p> <pre><code>InputStream is = getClass().getResourceAsStream("WebContent/WEB-INF/classes/testing/"+ COMPLETE_DOCUMENT + ".xml"); </code></pre> <p>of course the path you see in the method argument is the one to my file. I tried several combinations:</p> <pre><code>WebContent/WEB-INF/classes/testing/ classpath:testing/ classpath*:testing/ </code></pre> <p>but I always get the InputStream = null.</p> <p>I even tried to switch to</p> <pre><code> ClassLoader.getResourceAsStream(...) </code></pre> <p>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 </p> <pre><code>"classpath:folder/file.xsd" </code></pre> <p>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?</p> <p><strong>EDIT :</strong> 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:</p> <pre><code>ClassPathResource cpr = new ClassPathResource("xmlschemas/lrinode.xsd"); InputStream is = cpr.getInputStream(); </code></pre> <p>I Still get a <code>FileNotFound Exception</code>! Of course <code>"xmlschemas/lrinode.xsd"</code> 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.</p> <p>I'm starting to think I'm missing something trivial.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload