Download FAQ History |  | API Search Feedback

> ## Parsing a Parameterized DTD

> This section uses the Echo program to see what happens when you reference `xhtml.dtd` in `slideshow2.dtd`. It also covers the kinds of warnings that are generated by the SAX parser when a DTD is present.

> Note: The XML file used here is `slideSample08.xml`, which references `slideshow2.dtd`. The output is contained in `Echo10-08.txt`. (The browsable versions are `slideSample08-xml.html`, `slideshow2-dtd.html`, and `Echo10-08.html`.)

> When you try to echo the slide presentation, you find that it now contains a new error. The relevant part of the output is shown here (formatted for readability):

>     <?xml version='1.0' encoding='UTF-8'?>
>     ** Parsing error, line 22, uri: .../slideshow.dtd
>     Element type "title" must not be declared more than once.

> Note: The message above was generated by the JAXP 1.2 libraries. If you are using a different parser, the error message is likely to be somewhat different.

> The problem is that `xhtml.dtd` defines a `title` element which is entirely different from the `title` element defined in the slideshow DTD. Because there is no hierarchy in the DTD, these two definitions conflict.

> The `slideSample09.xml` version solves the problem by changing the name of the slide title. Run the Echo program on that version of the slide presentation. It should run to completion and display output like that shown in Echo10-09.

> Congratulations! You have now read a fully validated XML document. The change in that version of the file had the effect of putting the DTD's `title` element into a slideshow "namespace" that you artificially constructed by hyphenating the name, so the `title` element in the "slideshow namespace" (`slide-title`, really) was no longer in conflict with the `title` element in `xhtml.dtd`.

> Note: As mentioned in Using Namespaces, namespaces let you accomplish the same goal without having to rename any elements.

> To finish off this section, we'll take a look at the kinds of warnings that the validating parser can produce when processing the DTD.

> ### DTD Warnings

> As mentioned earlier in this tutorial, warnings are generated only when the SAX parser is processing a DTD. Some warnings are generated only by the validating parser. The nonvalidating parser's main goal is operate as rapidly as possible, but it too generates some warnings. (The explanations that follow tell which does what.)

> The XML specification suggests that warnings should be generated as result of:

> - Providing additional declarations for entities, attributes, or notations.

> (Such declarations are ignored. Only the first is used. Also, note that duplicate definitions of elements always produce a fatal error when validating, as you saw earlier.)

> - Referencing an undeclared element type.

> (A validity error occurs only if the undeclared type is actually used in the XML document. A warning results when the undeclared element is referenced in the DTD.)

> - Declaring attributes for undeclared element types.

> The Java XML SAX parser also emits warnings in other cases, such as:

> - No <!DOCTYPE ...> when validating.
> - Referencing an undefined parameter entity when not validating.

> (When validating, an error results. Although nonvalidating parsers are not required to read parameter entities, the Java XML parser does so. Since it is not a requirement, the Java XML parser generates a warning, rather than an error.)

> - Certain cases where the character-encoding declaration does not look right.

> At this point, you have digested many XML concepts, including DTDs, external entities. You have also learned your way around the SAX parser. The remainder of the SAX tutorial covers advanced topics that you will only need to understand if you are writing SAX-based applications. If your primary goal is to write DOM-based applications, you can skip ahead to Document Object Model.

Download FAQ History |  | API Search Feedback

All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.