File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes App chokes when SAX-parsing an XML file without a DTD file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "App chokes when SAX-parsing an XML file without a DTD file" Watch "App chokes when SAX-parsing an XML file without a DTD file" New topic
Author

App chokes when SAX-parsing an XML file without a DTD file

dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
I have a Swing app that stores its data in XML, reading the data back in with Xerces SAX. If my understanding is correct, then those XML data files need to have a DTD defined in the DOCTYPE tag, so I do that like this:


I don't want these data files to be validated by the SAX parser, so I specify as such like this:

However, I've found that even with validation set to false, I still need to provide a DTD for the parser. So in this case (according to my DOCTYPE tag), I need a DTD named "appprefs.dtd" in the same directory as my app's executable. To ensure the existence of that DTD file, before any parsing takes place, I have my app look for that file's existence and if it doesn't exist, writes it out.

The problem with that is that I've discovered that my approach doesn't ensure the DTD's existence. I've had some customers "lose data", and when reviewing their log files, have found that for whatever reasons they sometimes get IOExceptions of the "access denied" variety; they can't write the DTD file to that directory.

So... I'm kind of at a loss at the best way to fix this. Does anyone have a similar scenario that works? Is there a way that I can specify like an in-memory DTD? Or is there same way that I am unaware of that I can just do without providing a DTD altogether?

Thanks in advance.
[ April 27, 2005: Message edited by: dave taubler ]

Dave Taubler<br />Specializing in <a href="http://taubler.com/articles/" target="_blank" rel="nofollow">Java and Web Development</a>
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Hello Dave,
The parser will not write/create a DTD automatically if it doesnt find a dtd while parsing an xml.

For example the below will only parse the given xml and dont have anything to do with file writing.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
You should not have to provide a DTD period. When dealing with XML from known sources with SAX or DOM, I never have to use a DTD.
Bill
dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
Thank for the answers so far.

William, do you specify a DTD in your DOCTYPE tag, the way I did? Also, what parser do you use (if you're not using Xerces, then maybe yours behaves differently than Xerces?) My parser definitely refuses to parse when the DTD isn't there, but I'm obviously doing something different than you are.
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
I have used Xerces, it worked without dtd. Also the same for Crimson.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12787
    
    5
I work with XML documents that do not have a DOCTYPE declaration using the Java SDK library XML parsers in Java 1.4 and 1.5. The documents start:
<?xml version="1.0" encoding="UTF-8"?>
and then have the root element.
Bill
dave taubler
Ranch Hand

Joined: May 15, 2001
Posts: 132
Wow. Xerces just chokes when I don't provide a DTD. I removed the

declaration from my XML file, and now a NullPointerException gets thrown from Xerces. I'm trying to keep my app Java 1.3-compatable, but maybe it's time to move to 1.4 and use Java's parsers, and chuck Xerces. It really makes no sense that it would behave that way, though...
[ April 28, 2005: Message edited by: dave taubler ]
Cyril Lavy
Greenhorn

Joined: Nov 18, 2003
Posts: 7
Hello, having the same problem I decided to rewrite the file before parsing it. I use code below to be sure of the utf8 content :


the file is not modified when the parser begins, it leads to the same old java.net.UnknownHostException: because of the dtd. Have I done something wrong? thanks in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: App chokes when SAX-parsing an XML file without a DTD file