Java Reference
In-Depth Information
error()
org.xml.sax.SAXParseException;
lineNumber:
2;
columnNumber: 14; Document is
invalid:
no grammar found.
error()
org.xml.sax.SAXParseException;
lineNumber:
2;
columnNumber: 14; Document root
element "svg-examples", must match DOCTYPE root "null".
TheselinesrevealthataDTDgrammarhasnotbeenfound.Furthermore,theparser
reports a mismatch between
svg-examples
(it considers the first encountered ele-
ment to be the root element) and
null
(it considers
null
to be the name of the root
elementintheabsenceofaDTD).Neitherviolationisconsideredtobefatal,whichis
why
error()
is called instead of
fatalError()
.
Add the internal DTD to
svg-examples.xml
, and reexecute
java SAXDemo
svg-examples.xml v
. This time, you should see no
error()
-prefixed lines in
the output.
Note
SAX2validationdefaultstovalidatingagainstaDTD.Tovalidateagainstan
XML Schema-based schema instead, add the
schemaLanguage
property with the
Accomplish this task for
SAXDemo
by specifying
xm-
lr.setProperty("http://java.sun.com/xml/jaxp/properties/
schemaLanguage", "http://www.w3.org/2001/XMLSchema");
before
xmlr.parse(new InputSource(new FileReader(args[0])));
.
Creating a Custom Entity Resolver
While exploring XML, I introduced you to the concept of
entities
, which are aliased
data. I then discussed general entities and parameter entities in terms of their internal
and external variants.
Unlikeinternalentities,whosevaluesarespecifiedinaDTD,thevaluesofexternal
entitiesarespecifiedoutsideofaDTD,andareidentifiedviapublicand/orsystemiden-
tifiers. The system identifier is a URI whereas the public identifier is a formal public
identifier.
An XML parser reads an external entity (including the external DTD subset) via an
InputSource
instancethat'sconnectedtotheappropriatesystemidentifier.Inmany
cases, youpassasystem identifier or
InputSource
instance totheparser,andletit
discoverwheretofindotherentitiesthatarereferencedfromthecurrentdocumenten-
tity.