Using the Validating Parser

首先要明白,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 String JAXP_SCHEMA_LANGUAGE =
    "http://java.sun.com/xml/jaxp/properties/schemaLanguage";

static final String W3C_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本身。除了上述错误,经常是一些重复定义,或者是给未定义的元素定义属性等等这样的错误。
<think>好的,用户想了解关于“验证类”的概念或实现方式,特别是编程或框架中的class validation。首先我需要明确“验证类”指的是什么。在编程中,验证通常指确保输入数据符合特定规则或格式,比如用户注册时检查邮箱是否有效。而“验证类”可能是指专门负责这些验证逻辑的类,或是框架中提供的验证机制。 接下来,我要考虑不同编程语言或框架中的实现方式。例如,在Java中,可能有Bean Validation API(JSR 380),使用注解如@NotNull、@Email等。而在C#中,ASP.NET Core有数据注解特性,类似的方式。Python的Django框架也有表单验证机制。这些都是类级别的验证,通过注解或装饰器来实现。 另外,用户提到的引用内容中,引用[2]提到了在类的构造函数中调用基类构造函数,并重写Invoke方法处理参数。这可能涉及到方法参数的验证,比如在方法调用前检查参数是否符合要求,属于验证类的一种实现方式。需要将这点融入回答中,说明如何在类或方法层面进行参数验证。 还要考虑用户可能的实际需求,比如他们是否在开发Web应用,需要表单验证,或者是在构建API时需要请求参数验证。因此,回答中需要涵盖不同场景下的验证类实现,并给出具体的例子,如使用ASP.NET Core的数据注解或FluentValidation库。 另外,用户提供的引用[3]提到了监督学习中的数据预处理,虽然不直接相关,但数据验证也是数据预处理的一部分,可以间接联系到输入数据的清洗和验证,确保模型输入的正确性。不过这可能不是用户当前问题的重点,但可以作为扩展点。 最后,生成相关问题时需要考虑用户可能进一步想了解的内容,比如验证类的设计模式、如何自定义验证规则、常见框架中的验证机制比较等。需要确保问题具有引导性,帮助用户深入探索该主题。</think>### 关于验证类(Class Validation)的概念与实现 在编程或框架中,“验证类”通常指用于对输入数据、对象属性或方法参数进行规则校验的组件。其核心目标是确保数据符合业务逻辑、安全要求或技术规范。以下是其常见实现方式及示例: --- #### 1. **基于注解的验证(Annotation-Based Validation)** - **概念**:通过注解(Annotation)或装饰器(Decorator)直接在类属性上定义验证规则。 - **框架示例**: - **Java (JSR 380 Bean Validation)**: 使用`@NotNull`, `@Email`等注解。 ```java public class User { @NotNull @Email private String email; } ``` - **C# (ASP.NET Core Data Annotations)**: 使用`[Required]`, `[StringLength]`等特性。 ```csharp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值