SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); XMLReader reader = spf.newSAXParser().getXMLReader(); ValidatorHandler vh = schema.newValidatorHandler(); //key is to set "ValidatorHandler" as ContentHandler //so that SAX event can be validated reader.setContentHandler(vh); reader.parse(xml);
2.运行ValidationFramework 目录中的ant图标。要编译使用如下的命令: ant compile
反馈信息中,你会看到如下的内容: Buildfile: build.xml
init:
[mkdir] Created dir: C:/ValidationFramework/build [mkdir] Created dir: C:/ValidationFramework/build/classes
compile:
[echo] C:/Program Files/Java/jdk1.5.0/jre ...
BUILD SUCCESSFUL
要运行例子,利用适当的目标来运行ant命令,例如: ant ValidateSAXStream
在反馈的信息中,你会看到如下的信息输出: [java] startElement: personnel [java] startElement: person [java] startElement: name [java] startElement: family [java] characters: Boss [java] endElement: family ...
[java] startElement: email [java] characters: five@foo.com [java] endElement: email [java] startElement: link [java] endElement: link [java] endElement: person [java] endElement: personnel
BUILD SUCCESSFUL
假如你在J2SE 5.0中运行例子,不考虑JAXP jar的位置的'endorsed' 属性。例如: ant -Dendorsed=/space/jaxp/jaxp-1_3/dist/ Validate