• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

DTD validation

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am using the Xerces2 for Java XML parser just now and I am looking for a way to validate an XML document using a DTD. The previous piece of code that I am looking at manipulates the XML string to include the <!DOCTYPE> tag with the DTD location before it is loaded into the parser.

However, this seems clunky, expensive and I feel that has to be better way of doing this.

What is the best way of dynamically attaching a DTD to an XML document so that it can be parsed using Xerces as the parser?

I've seen this done using the grammar pool property when using a schema, but I am not sure whether the same can be done with DTDs.

Any thoughts?

Thanks,


Stuart
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a code example that validates XML against an external schema. It should work the same for DTDs if you substitute an appropriate value for the "schemaLanguage" property (I'm not sure what that is off the top of my head).
 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf,


Thanks for your response. That is along the lines of what I am looking for. If anyone else is reading this, do they know what the setting is to invoke DTD validation rather than schema?

Thanks,


Stuart
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll find the answer to that in the javadocs of the javax.xml.XMLConstants class.
 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf....however.

Not an encouraging point from the following article (
http://www.onjava.com/pub/a/onjava/excerpt/learnjava_23/index4.html#49809):

To use our DTD, we must associate it with the XML document. We do this by placing a DOCTYPE declaration in the XML itself. When a validating parser encounters the DOCTYPE, it attempts to load the DTD and validate the document. There are several forms the DOCTYPE can have, but the one we'll use is:

<!DOCTYPE Inventory SYSTEM "zooinventory.dtd">
Both SAX and DOM parsers can automatically validate documents that contain a DOCTYPE declaration. However, you have to explicitly ask the parser factory to provide a parser that is capable of validation. To do this, set the validating property of the parser factory to true before you ask it for an instance of the parser. For example:
SAXParserFactory factory = SAXParserFactory.newInstance( );
factory.setValidating( true );

----------------

Which further states "Although DTDs can define the basic structure of an XML document, they can't adequately describe data and validate it programmatically."

Unless I am missing a trick of course.


Stuart
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that DTDs are much less expressive than "real" schemas should not be a surprise. But the operative word is "adequately" - as far as they can describe XML data, DTDs can be used to validate it.

I think what's described in that paragraph is a slightly different use case, where the document contains a DOCTYPE, and from that the file to examine is inferred. But the code example specifically tells the parser where to find the DTD, so I think both methods are possible. Have you tried it?
[ May 28, 2007: Message edited by: Ulf Dittmer ]
 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf,

I am aware that schemas are more versatile, modern and comprehensive than DTDs. Unfortunately, the solution we are having to use only allows for DTDs, so we are stuck with them for the moment.

I haven't tried the code yet for programmatic DTD validation, but I will spend sometime tomorrow looking at it. It just doesn't seem that clear cut and all the examples are based on schemas rather than DTDs.

I'll let you know how I get on.


Stuart
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stuart,

Yours in an interesting question and one that I'd like to be able to help with. But there's one thing that confuses me (which is not that hard to do since I'm quite new to XML).

Since both the xerces SAX and DOM parsers can validate an XML document instance against a DTD (to the extent that DTDs allow), is this not adequate for your application? What I mean to ask is: what further specific validation to you require that the SAX and DOM validating parsers do not provide?

Thanks, Warren.
 
Sheriff
Posts: 28382
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Warren, the problem that Stuart has (and apparently a lot of other people, because I see this question quite regularly) is that his XML document doesn't contain a reference to a DTD, so even a validating parser won't apply any DTD when it parses the document. The question is how to force the parser to use a particular DTD even when the document doesn't reference it.
[ May 29, 2007: Message edited by: Paul Clapham ]
 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

You are essentially correct. I am looking to configure the parser to tell it where to get a DTD and include it for validation. This would save XML string manipulation (to include the DTD reference) every time the XML is due to be parsed. I would have thought that configuring it at the parser level would have performance advantages too.

I am not concerned about how much validation a DTD gives me in comparison to schemas etc. If I had a choice I would use a schema, but I don't.

Paul, you mentioned that you have seen this question many times, has anyone found an answer? The link that Ulf provided is very much schema-centric and it is taking a bit of time to find out the means to do this using a DTD instead.

Thanks,


Stuart
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart J W Bell:

Paul, you mentioned that you have seen this question many times, has anyone found an answer? The link that Ulf provided is very much schema-centric and it is taking a bit of time to find out the means to do this using a DTD instead.



The example code performs validation in general; the only schema-specific part of it is the line that selects which schema language to use. The XMLConstants javadocs tell you what to substitute for a DTD. Testing this with your case can't possibly take longer than 5 minutes at the most. So I'm a bit at a loss to understand what else you are looking for.
 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've managed to make use of the EntityResolver interface to re-direct the DTD reference to a local location. Which is really what I was trying to do programmatically.

An example of this is shown below.

Thanks for all your input.


Stuart

------------------------------------------

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

public class DtdEntityResolver implements EntityResolver {

public InputSource resolveEntity(String publicID, String systemID) {

if (systemID.equals("Quote_21.dtd")) {
return new InputSource("/url/goes/here/Quotes_21.dtd");
}
else {
// use the default behaviour
return null;
}
}
}
 
Paul Clapham
Sheriff
Posts: 28382
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart J W Bell:
I've managed to make use of the EntityResolver interface to re-direct the DTD reference to a local location.

Yes, that's the standard method to use if your document already contains a DTD reference. But unfortunately you never said that, and many of the things you did say implied that it wasn't the case. My assumption from the beginning was that your document didn't have a DTD reference. Funny how little mis-communications like that can lead you down the wrong path.
 
Stuart J W Bell
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies for any misdirection, it wasn't intended.

Regards,


Stuart
 
Paul Clapham
Sheriff
Posts: 28382
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, and I should have posted the EntityResolver idea too.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic