首先要明白,Validating Parser是用什么来验证的。
You need to understand about two things about the validating parser at the outset:
A schema or document type definition (DTD) is required.
Because the schema or DTD is present, the
ignorableWhitespace
method is invoked whenever possible.
To be notified of validation errors in an XML document, the parser factory must be configured to create a validating parser, as shown in the preceding section. In addition, the following must be true:
The appropriate properties must be set on the SAX parser.
The appropriate error handler must be set.
The document must be associated with a schema.
注意这三点。
Setting the SAX Parser Properties
It's helpful to start by defining the constants you'll use when setting the properties:
static final StringJAXP_SCHEMA_LANGUAGE
= "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; static final StringW3C_XML_SCHEMA
= "http://www.w3.org/2001/XMLSchema";
SAXParserFactory factory = SAXParserFactory.newInstance();factory.setNamespaceAware(true);
factory.setValidating(true);
You'll learn more about namespaces in . For now, understand that schema
validation is a namespace-oriented process. Because JAXP-compliant parsers are not namespace-aware
by default, it is necessary to set the property for schema validation to work.
saxParser.setProperty(JAXP_SCHEMA_LANGUAGE
,W3C_XML_SCHEMA
);
Experimenting with Validation Errors
Validating Parsing主要处理下面的错误
1、XML文档中没有一个有效的验证文档,比如说没有一个有效的dtd文件,DOCTYPE标签的缺失。
2、XML文档的内容和dtd描述不相符,比如(image?,title,item*)
,如果xml中该元素的中缺失title就会报错。
3、PCDATA的问题,或者是说元素当中可不可以含有元素和字符的混合体。
DTD Warnings
# 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.
# References to an undefined parameter entity when not validating. #Certain cases where the character-encoding declaration does not look right.
注意:Validating Error和DtD Warnings中的区别和联系。
1、如果XML没有根据dtd的限制进行编写,比如多个dtd中没有声明的元素,是errors(),反过来如果是dtd中定义的元素或者是属性没有在xml中有所体现,那么就会有warning。
2、warning大部分是发生在dtd本身。除了上述错误,经常是一些重复定义,或者是给未定义的元素定义属性等等这样的错误。

