Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
dave taubler
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used Xerces, it worked without dtd. Also the same for Crimson.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic