关于用Schema校验XML的问题?

shcyb2000 03-08-01

运行下面这些代码,只要XML文件符合XML规范都能通过,而不会校验是否符合Schema的定义。为什么?

// 在此输入java代码
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";
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";
static final String MY_SCHEMA_FILE =
"cd_catalog_schema.xml";


public static void main(String argv[])
{
/* Here is some constant used to set properties of the SAX parser. */
try{


DefaultHandler handler = new ParseSchemaXml();

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();

saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
String file = MY_SCHEMA_FILE;
saxParser.setProperty(JAXP_SCHEMA_SOURCE, new File(file));

File xmlFile = new File(
"cd_catalog.xml");
saxParser.parse(xmlFile, handler);


System.exit(0);
}catch(Exception er){
System.out.println(er);
}
}

banq
2003-08-01 18:27

这是XML最简单的使用,一般都不会去和Schema检验。

除非你做很抽象的东西,比如类似ofbiz那样的数据交换等工具软件

shcyb2000
2003-08-04 09:11

那有什么办法加上强制检查吗?