You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Robert William Vesterman <bo...@digitalarts.com> on 2005/10/17 20:17:25 UTC
Ignoring DTDs
I am using Xerces-C SAX2 to parse some documents that I write. When I write
them, I would like to include a reference to an appropriate DTD in a
doctypedecl. However, I want to do so basically for the purposes of people
looking at the documents, or trying to build their own. I do *not* want
Xerces to pay attention to the DTD while parsing the XML, and I *especially*
do not want Xerces to try to fetch the DTD for any purpose (this application
will be running on non-internet enabled machines, and speed is of the
essence).
So, essentially, I would like to know if it's possible to set up Xerces-C
SAX2 to ignore a well-formed doctypedecl, and if so, how.
Again, please note that by "ignore", I mean more than just (for example) "If
I don't set a DTD handler, Xerces won't pass me notification of DTD events".
Whether or not I get notified of DTD events is not the issue - it's only a
side effect. The issue is that I don't want Xerces going and trying to
fetch the DTD in the first place.
Thanks in advance for any help.
Bob Vesterman.
Re: Ignoring DTDs
Posted by David Bertoni <db...@apache.org>.
Robert William Vesterman wrote:
> I am using Xerces-C SAX2 to parse some documents that I write. When I write
> them, I would like to include a reference to an appropriate DTD in a
> doctypedecl. However, I want to do so basically for the purposes of people
> looking at the documents, or trying to build their own. I do *not* want
> Xerces to pay attention to the DTD while parsing the XML, and I *especially*
> do not want Xerces to try to fetch the DTD for any purpose (this application
> will be running on non-internet enabled machines, and speed is of the
> essence).
>
> So, essentially, I would like to know if it's possible to set up Xerces-C
> SAX2 to ignore a well-formed doctypedecl, and if so, how.
>
You can use the EntityResolver mechanism to control external entities.
Whenever the parser needs an external entity, it will call your
implementation of EntityResolver. If you return a null pointer, the
parser will attempt to dereference the system ID of the entity. If you
return a InputSource instance, it will use that to read the entity. You
can always return an InputSource that is "empty" if you want the DTD to
be empty.
However, if the instance document requires the DTD, because it contains
references to other entities defined in the DTD, you cannot return an
"empty" InputSource. Note also that you can change the content of the
document by not including the DTD. For example, the DTD might specify
default attributes, which would not be added if you don't let the parser
read the DTD.
I think if you search the mailing list archives for "EntityResolver",
you'll find more information.
Dave
Re: [OBORONA-SPAM] Ignoring DTDs
Posted by pps <i-...@yandex.ru>.
Robert William Vesterman wrote:
> I am using Xerces-C SAX2 to parse some documents that I write. When I write
> them, I would like to include a reference to an appropriate DTD in a
> doctypedecl. However, I want to do so basically for the purposes of people
> looking at the documents, or trying to build their own. I do *not* want
> Xerces to pay attention to the DTD while parsing the XML, and I *especially*
> do not want Xerces to try to fetch the DTD for any purpose (this application
> will be running on non-internet enabled machines, and speed is of the
> essence).
>
> So, essentially, I would like to know if it's possible to set up Xerces-C
> SAX2 to ignore a well-formed doctypedecl, and if so, how.
>
> Again, please note that by "ignore", I mean more than just (for example) "If
> I don't set a DTD handler, Xerces won't pass me notification of DTD events".
> Whether or not I get notified of DTD events is not the issue - it's only a
> side effect. The issue is that I don't want Xerces going and trying to
> fetch the DTD in the first place.
>
> Thanks in advance for any help.
>
> Bob Vesterman.
>
As far as I understood you, you want some control on processing external
dtd's. I needed similar control, so I did it this way - I overloaded
resolveEntity of XercesDOMParser like this:
InputSource*
dom_parser::resolveEntity(XMLResourceIdentifier* rId){
const XMLCh *pub_id = rId->getPublicId();
if(pub_id){
dtd_map_t::const_iterator i=dtds.find(pub_id);
if(i!=dtds.end()){
return new LocalFileInputSource(i->second.c_str());
}
}
return 0;
}
where dtd_map_t dtds is map<string,string> that maps certain public ids
to local files; something like this:
dtds["-//W3C//DTD XHTML 1.0 Strict//EN"] = "dtd/Strict1_0.dtd";
dtds["-//W3C//DTD XHTML 1.0 Frameset//EN"] = "dtd/Frameset1_0.dtd";
dtds["-//W3C//DTD XHTML 1.0 Transitional//EN"] = "dtd/Transitional1_0.dtd";
resolveEntity returns 0 for default processing (fetching file), you may
return new LocalFileInputSource("dummy.dtd"); if you want to ignore
remote dtd
Between, there might be better ways to do it, I tried to ask this list,
but nobody even replied me, so I figured out this on my own how to do
what I needed (after wasting a lot of time - no docs, tutorials, etc...)