Convert XML file into InputStream

Discussion in 'Java' started by Ale, Oct 29, 2009.

  1. Ale

    Ale Guest

    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
     
    Ale, Oct 29, 2009
    #1
    1. Advertising

  2. Ale

    Lew Guest

    Ale wrote:
    > 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()'.

    --
    Lew
     
    Lew, Oct 29, 2009
    #2
    1. Advertising

  3. Ale

    markspace Guest

    Ale wrote:
    > 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.
     
    markspace, Oct 29, 2009
    #3
  4. Ale

    Lew Guest

    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()'.

    --
    Lew
    Now where have I heard that before?
     
    Lew, Oct 29, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    663
  2. Ahmed Moustafa

    How to convert String to InputStream?

    Ahmed Moustafa, Sep 25, 2003, in forum: Java
    Replies:
    5
    Views:
    104,512
    Roedy Green
    Sep 25, 2003
  3. R
    Replies:
    5
    Views:
    2,146
    Kevin McMurtrie
    Mar 13, 2005
  4. Harry Zoroc
    Replies:
    1
    Views:
    992
    Gregory Vaughan
    Jul 12, 2004
  5. Replies:
    1
    Views:
    1,851
    Daniele Futtorovic
    Jul 1, 2008
Loading...

Share This Page