Convert XML file into InputStream

A

Ale

Hi all,

I need to convert an XML file into an InputStream in order to have an
object and do other work.

This is the fragment, but XML file can't be found (it's of course in
the classpath, I've also tried using an absolute path name):

//START
InputStream stream;
stream = ClassLoader.getSystemResourceAsStream("/com/home/resources");

JAXBContext context = JAXBContext.newInstance(GetChartRequest1.class);
Unmarshaller unmarsh = context.createUnmarshaller();
GetChartRequest1 req = (GetChartRequest1)unmarsh.unmarshal(stream);
//END

Any suggestion ?

Thanks and best regards,
Alex
 
L

Lew

Ale said:
Hi all,

I need to convert an XML file into an InputStream in order to have an
object and do other work.

This is the fragment, but XML file can't be found (it's of course in
the classpath, I've also tried using an absolute path name):

//START
InputStream stream;
stream = ClassLoader.getSystemResourceAsStream("/com/home/resources");

JAXBContext context = JAXBContext.newInstance(GetChartRequest1.class);
Unmarshaller unmarsh = context.createUnmarshaller();
GetChartRequest1 req = (GetChartRequest1)unmarsh.unmarshal(stream);
//END

Don't use 'ClassLoader.getSystemResourceAsStream()', use
'ClassLoader.getResourceAsStream()' or 'Class.getResourceAsStream()'.
 
M

markspace

Ale said:
Hi all,

I need to convert an XML file into an InputStream in order to have an
object and do other work.

This is the fragment, but XML file can't be found (it's of course in
the classpath, I've also tried using an absolute path name):


File not found == it ain't there.

Can you show us the "absolute path" you used? Can you show us a
directory listing from the command prompt to prove that the file really
is where you say it is?

Otherwise, the system is correct, it ain't there.


Other ideas:

Is this a Jar file? Jar files ignore the class path variable.

Is the "resource" inside the Jar file? Or loose on disc? If it's
inside the Jar file, don't use ClassLoader or Class. use getClass().

getClass().getResourceAsStream( "..." );

I just noticed your are calling "getSystemResourceAsStream". That's
almost certainly wrong. System resources live inside the Java JRE, in
rt.jar or some such. If your resource is anywhere else on disc, you
can't get it with getSystemResourceAsStream().


More info please... show us the command line you use to execute the app,
the location of the resource, the class path, etc. Cut and paste
everything directly from your computer screen to your email, so there's
no typos. We have to know exactly what's going on to fix this for you.
 
L

Lew

markspace <wrote:
I just noticed your are calling "getSystemResourceAsStream".  That's
almost certainly wrong.  System resources live inside the Java JRE, in
rt.jar or some such.  If your resource is anywhere else on disc, you
can't get it with getSystemResourceAsStream().

In other words:
Don't use 'ClassLoader.getSystemResourceAsStream()', use
'ClassLoader.getResourceAsStream()' or 'Class.getResourceAsStream()'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top