SAX and file chooser

S

steve_marjoribanks

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
 
T

Thomas Fritsch

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());
 
S

steve_marjoribanks

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
 
O

Oliver Wong

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
 
S

steve_marjoribanks

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
 
O

Oliver Wong

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
 

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

Similar Threads


Members online

Forum statistics

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

Latest Threads

Top