SAX and file chooser

Discussion in 'Java' started by steve_marjoribanks@hotmail.com, Feb 15, 2006.

  1. Guest

    How can I use SAX to parse an XML document which has been selected
    using a file chooser? I want to use an InputSource for the XMLReader to
    parse as this allows the parser to resolve relative paths within the
    XML document (ie. paths of schemas etc). However, as far as I can
    understand from what I've read (and I'm new to this so it probably
    isn't very far!), the InputSource takes a URI string as an input but
    the JFileChooser class outputs a File object. How can I load an XML
    document using the JFileChooser class and then parse it using an
    InputSource?
    Thanks in advance for any help,

    Steve
     
    , Feb 15, 2006
    #1
    1. Advertising

  2. wrote:
    > How can I use SAX to parse an XML document which has been selected
    > using a file chooser? I want to use an InputSource for the XMLReader to
    > parse as this allows the parser to resolve relative paths within the
    > XML document (ie. paths of schemas etc). However, as far as I can
    > understand from what I've read (and I'm new to this so it probably
    > isn't very far!), the InputSource takes a URI string as an input but
    > the JFileChooser class outputs a File object. How can I load an XML
    > document using the JFileChooser class and then parse it using an
    > InputSource?

    See <http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/InputSource.html>

    InputSource has several constructors: The one you mentioned (taking an
    URI-string), and 2 others (taking an InputStream, or a Reader).
    So you can choose what you like:
    File file = ...; // from your JFileChooser
    InputSource src = new InputSource(new FileInputStream(file));
    or
    InputSource src = new InputSource(new FileReader(file));
    or
    InputSource src = new InputSource(file.toURI());



    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Feb 15, 2006
    #2
    1. Advertising

  3. Thomas Fritsch wrote:
    > InputSource src = new InputSource(file.toURI());

    oops! should be
    InputSource src = new InputSource(file.toURI().toString());

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Feb 15, 2006
    #3
  4. Guest

    Ok, thanks very much! I'll give it a go

    Steve
     
    , Feb 16, 2006
    #4
  5. Guest

    Am I correct in thinking that if I choose to use the code you gave an
    example of above using a URI string eg:

    InputSource input = new InputSource(file.toURI().toString());

    that I do not need to 'close' anything when I have finished using the
    file? In the API it says,

    'An InputSource object belongs to the application: the SAX parser shall
    never modify it in any way (it may modify a copy if necessary).
    However, standard processing of both byte and character streams is to
    close them on as part of end-of-parse cleanup, so applications should
    not attempt to re-use such streams after they have been handed to a
    parser.'

    Does this mean that it is not necessary to 'close' anything as you
    would do if you were using a BufferedReader or an InputStream? Or if
    you do need to take some action when you have finished with an
    InputSource, how do you go about it?

    Thanks

    Steve
     
    , Feb 16, 2006
    #5
  6. Oliver Wong Guest

    <> wrote in message
    news:...
    > Am I correct in thinking that if I choose to use the code you gave an
    > example of above using a URI string eg:
    >
    > InputSource input = new InputSource(file.toURI().toString());
    >
    > that I do not need to 'close' anything when I have finished using the
    > file? In the API it says,
    >
    > 'An InputSource object belongs to the application: the SAX parser shall
    > never modify it in any way (it may modify a copy if necessary).
    > However, standard processing of both byte and character streams is to
    > close them on as part of end-of-parse cleanup, so applications should
    > not attempt to re-use such streams after they have been handed to a
    > parser.'
    >
    > Does this mean that it is not necessary to 'close' anything as you
    > would do if you were using a BufferedReader or an InputStream? Or if
    > you do need to take some action when you have finished with an
    > InputSource, how do you go about it?


    Maybe it's better if you explain what you intend to do after you have
    finished with an InputSource. See
    http://www.catb.org/~esr/faqs/smart-questions.html#goal

    - Oliver
     
    Oliver Wong, Feb 16, 2006
    #6
  7. Guest

    Ok, sorry.
    Basically I am writing an application which opens an XML file, parses
    it (including validation against a remote schema) and then eventually
    displays the data contained within the XML file pictorially (though I
    haven't got onto this bit yet).
    So far I have an application which can load a file and parse the XML
    file (and validate it). At the moment I am running it from the command
    line but the idea is that eventually I will create an executable jar
    file.
    Currently I have it so that only one file can be open at any one time
    although eventually I intend to make it possible to have multiple files
    open at once (using internal frames and desktop panes). Therefore, the
    user will be able to close files without closing the application as a
    whole. Therefore, I imagine it is fairly important to make sure that
    inputs which have been finished with are 'closed' properly otherwise
    you would end up with lots of inputs open not doing anything which
    would slow it down.
    I am aware that with classes such as BufferedReader there are methods
    like close() which close that particular input. I am using a SAX
    InputSource as shown in my previous post but after consulting the API I
    found that there isn't a 'close' method or equivalent for this. In the
    above post I was enquiring as to what the little snippet from the API I
    posted actually meant and did I need to take any further action to
    'close' the input.
    Regards,

    Steve
     
    , Feb 17, 2006
    #7
  8. Oliver Wong Guest

    <> wrote in message
    news:...
    > Ok, sorry.
    > Basically I am writing an application which opens an XML file, parses
    > it (including validation against a remote schema) and then eventually
    > displays the data contained within the XML file pictorially (though I
    > haven't got onto this bit yet).
    > So far I have an application which can load a file and parse the XML
    > file (and validate it). At the moment I am running it from the command
    > line but the idea is that eventually I will create an executable jar
    > file.
    > Currently I have it so that only one file can be open at any one time
    > although eventually I intend to make it possible to have multiple files
    > open at once (using internal frames and desktop panes). Therefore, the
    > user will be able to close files without closing the application as a
    > whole. Therefore, I imagine it is fairly important to make sure that
    > inputs which have been finished with are 'closed' properly otherwise
    > you would end up with lots of inputs open not doing anything which
    > would slow it down.
    > I am aware that with classes such as BufferedReader there are methods
    > like close() which close that particular input. I am using a SAX
    > InputSource as shown in my previous post but after consulting the API I
    > found that there isn't a 'close' method or equivalent for this. In the
    > above post I was enquiring as to what the little snippet from the API I
    > posted actually meant and did I need to take any further action to
    > 'close' the input.
    > Regards,


    Sorry, it looks like I misunderstood what you wrote earlier:

    <quote>
    if
    you do need to take some action when you have finished with an
    InputSource, how do you go about it?
    </quote>

    I thought you meant you wanted to do something after the XML parser
    finishes parsing, but before it closes the file, and you were asking how to
    prevent the XML parser from closing the file.

    My interpretation of the documentation is that the parser will close the
    stream for you automatically. If you're really nervous about it though, what
    I would do in your situation is to try to close the file anyway. And if an
    exception gets thrown, like FileAlreadyClosedException or something similar,
    then I'd just remove the code that closes the file twice.

    - Oliver
     
    Oliver Wong, Feb 17, 2006
    #8
    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. =?Utf-8?B?Unlv?=

    File Chooser...

    =?Utf-8?B?Unlv?=, Sep 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,263
    =?Utf-8?B?UHVQdQ==?=
    Dec 2, 2005
  2. Szymon Madejczyk
    Replies:
    2
    Views:
    560
    Szymon Madejczyk
    Sep 15, 2003
  3. Robert M. Gary

    URL file chooser

    Robert M. Gary, Jan 31, 2006, in forum: Java
    Replies:
    2
    Views:
    561
    Robert M. Gary
    Jan 31, 2006
  4. Replies:
    0
    Views:
    377
  5. Agent Smith

    Using File Chooser Under NetBeans

    Agent Smith, Jun 9, 2008, in forum: Java
    Replies:
    6
    Views:
    1,163
    Andrew Thompson
    Jun 29, 2008
Loading...

Share This Page