Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   SAX and file chooser (http://www.velocityreviews.com/forums/t150288-sax-and-file-chooser.html)

steve_marjoribanks@hotmail.com 02-15-2006 05:30 PM

SAX and file chooser
 
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


Thomas Fritsch 02-15-2006 06:39 PM

Re: SAX and file chooser
 
steve_marjoribanks@hotmail.com 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 02-15-2006 06:45 PM

Re: SAX and file chooser
 
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(' $','@')

steve_marjoribanks@hotmail.com 02-16-2006 11:05 AM

Re: SAX and file chooser
 
Ok, thanks very much! I'll give it a go

Steve


steve_marjoribanks@hotmail.com 02-16-2006 08:52 PM

Re: SAX and file chooser
 
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


Oliver Wong 02-16-2006 10:52 PM

Re: SAX and file chooser
 

<steve_marjoribanks@hotmail.com> wrote in message
news:1140123172.365163.41080@f14g2000cwb.googlegro ups.com...
> 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


steve_marjoribanks@hotmail.com 02-17-2006 12:12 PM

Re: SAX and file chooser
 
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


Oliver Wong 02-17-2006 04:49 PM

Re: SAX and file chooser
 

<steve_marjoribanks@hotmail.com> wrote in message
news:1140178362.734645.243640@g43g2000cwa.googlegr oups.com...
> 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



All times are GMT. The time now is 05:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.