Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

SAX and file chooser

Thread view: 
steve_marjoribanks@hotmail.com - 15 Feb 2006 17:30 GMT
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 - 15 Feb 2006 18:39 GMT
> 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
[quoted text clipped - 5 lines]
> 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());

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Thomas Fritsch - 15 Feb 2006 18:45 GMT
>    InputSource src = new InputSource(file.toURI());
oops! should be
     InputSource src = new InputSource(file.toURI().toString());

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

steve_marjoribanks@hotmail.com - 16 Feb 2006 11:05 GMT
Ok, thanks very much! I'll give it a go

Steve
steve_marjoribanks@hotmail.com - 16 Feb 2006 20:52 GMT
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 - 16 Feb 2006 22:52 GMT
> Am I correct in thinking that if I choose to use the code you gave an
> example of above using a URI string eg:
[quoted text clipped - 15 lines]
> 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 - 17 Feb 2006 12:12 GMT
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 - 17 Feb 2006 16:49 GMT
> Ok, sorry.
> Basically I am writing an application which opens an XML file, parses
[quoted text clipped - 21 lines]
> '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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.