Beginner Question: How to access an XML file inside a JAR without extracting it

Discussion in 'Java' started by SK, Dec 9, 2003.

  1. SK

    SK Guest

    Hi,

    I am using JDOM to parse the XML files. I have a file inside a JAR
    file that I want to read but without extracting it from the JAR
    itself. Can anyone send me some code which does it.

    Thanks for the help in advance.
    - SK.
     
    SK, Dec 9, 2003
    #1
    1. Advertising

  2. SK

    Mike Guest

    (SK) wrote in message news:<>...

    The idea is that if the class (the code) was able to be loaded from
    the jar file, then you ought to be able to ask that same classloader
    that loaded the class to load something else for you. Like so:

    InputStream is = getClass().getClassLoader().getResourceAsStream("something.xml");

    Now you have an input stream. I'm sure there are a few API's
    available that can then load the DOM from the input stream. In fact,
    I'm doing it using JDOM all the time. JDOM rocks.


    > Hi,
    >
    > I am using JDOM to parse the XML files. I have a file inside a JAR
    > file that I want to read but without extracting it from the JAR
    > itself. Can anyone send me some code which does it.
    >
    > Thanks for the help in advance.
    > - SK.
     
    Mike, Dec 10, 2003
    #2
    1. Advertising

  3. "SK" <> wrote in message
    news:...
    > Hi,
    >
    > I am using JDOM to parse the XML files. I have a
    > file inside a JAR file that I want to read but without
    > extracting it from the JAR itself. Can anyone send me
    > some code which does it.
    >


    Use:

    * Class.getResourceAsStream to extract your XML file
    as a byte sequence from the JAR

    * Parse using the appropriate method which accepts an
    'InputStream' as argument i.e.

    DocumentBuilder db =
    DocumentBuilderFactory.newDocumentBuilder();

    db.parse(theInputStream);

    Whenever accessing a JAR you are always *extracting* data. Indeed, the
    essence of a 'read' task is extraction. Even using the 'jar' utitlity to
    list JAR file contents sees a sequence of bytes 'extracted' in order to
    obtain the relevant data. The difference is, however, whether that data is
    immediately 'consumed' by the program [as would be the case when parsing an
    XML document stored in a JAR], or perhaps used to create other entities like
    files.

    I hope this helps.

    Anthony Borla
     
    Anthony Borla, Dec 10, 2003
    #3
  4. SK

    Chris Guest

    The XML file is in the classpath of your app as its part of the JAR,
    so you can use Class.getResourceAsStream to get an input-stream to it,
    which you can then pass into the SAX builder.

    - sarge
     
    Chris, Dec 10, 2003
    #4
  5. SK

    SK Guest

    Thanks All for your reply. It was really helpful. I did get it to work.

    - SK.
     
    SK, Dec 10, 2003
    #5
  6. SK

    Joined:
    Sep 19, 2009
    Messages:
    1
    Location:
    Los Angeles, CA
    The code works

    I tried the code suggested in this thread and it works perfectly. I can run a Java application from a jar file and access a XML file archived within the jar. It surprises me that an object of an abstract class, InputStream, can be set up this way. The getClass() method returns a class object, in this case, the input stream representing the archived XML file. Is this just a reference to the input stream? How can this work?
     
    , Sep 20, 2009
    #6
    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. lucy
    Replies:
    6
    Views:
    30,889
    steve
    Sep 7, 2004
  2. JML
    Replies:
    3
    Views:
    1,279
    Andrew Thompson
    Oct 26, 2004
  3. harry
    Replies:
    1
    Views:
    528
    Sudsy
    Nov 22, 2004
  4. Arnold Peters
    Replies:
    0
    Views:
    610
    Arnold Peters
    Jan 5, 2005
  5. muttley
    Replies:
    0
    Views:
    2,767
    muttley
    Oct 20, 2005
Loading...

Share This Page