Java 使用ApacheComos配置根据xsd验证xml
我是XML新手 我使用ApacheCommonsXMLConfiguration进行配置 我需要一个基于模式的验证 xsd位于我的java项目的resources文件夹中 xml和xsd不在同一位置 遵循(该报告仅为DTD示例)。我生成以下代码:Java 使用ApacheComos配置根据xsd验证xml,java,xml,xsd,xsd-validation,apache-commons-config,Java,Xml,Xsd,Xsd Validation,Apache Commons Config,我是XML新手 我使用ApacheCommonsXMLConfiguration进行配置 我需要一个基于模式的验证 xsd位于我的java项目的resources文件夹中 xml和xsd不在同一位置 遵循(该报告仅为DTD示例)。我生成以下代码: URL urlXsd = getClass().getResource("config.xsd"); DefaultEntityResolver resolver = new DefaultEntityResolver(); r
URL urlXsd = getClass().getResource("config.xsd");
DefaultEntityResolver resolver = new DefaultEntityResolver();
resolver.registerEntityId("configuration", urlXsd);
XMLConfiguration xmlConfig = new XMLConfiguration();
xmlConfig.setEntityResolver(resolver);
xmlConfig.setSchemaValidation(true);
xmlConfig.load(new File("config.xml"));
我得到以下例外:
Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 16; cvc-elt.1: Cannot find the declaration of element 'configuration'.
My config.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="configuration">
<xs:complexType>
<xs:sequence>
<xs:element name="reader">
</xs:element>
<xs:element name="writer">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
My config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
<reader>
</reader>
<writer>
</writer>
</configuration>
有人能帮我吗?我也遵循了文档(也是在当前的Apache Commons配置2中),在这种情况下加载模式不起作用。在文件的底部,它指出: Commons配置所提供的机制有望 然而,在大多数情况下,这是足够的 情况并非如此。XMLConfiguration提供了两个 应为应用程序提供所有 他们可能需要灵活性。第一,注册自定义实体 在前面的章节中已经讨论了解析器。这个 其次,XMLConfiguration提供了一种通用的设置方法 要使用的XML解析器:可以使用预配置的DocumentBuilder对象 传递给setDocumentBuilder()方法 我建议您使用“setDocumentBuilder”,而不是像这样的“setEntityResolver”:
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new File("C:\\config.xsd"));
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setSchema(schema);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
//if you want an exception to be thrown when there is invalid xml document,
//you need to set your own ErrorHandler because the default
//behavior is to just print an error message.
docBuilder.setErrorHandler(new ErrorHandler() {
@Override
public void warning(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void error(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
});
conf = new BasicConfigurationBuilder<>(XMLConfiguration.class).configure(new Parameters().xml()
.setFileName("config.xml")
.setDocumentBuilder(docBuilder)
.setSchemaValidation(true)
.setExpressionEngine(new XPathExpressionEngine())
).getConfiguration();
FileHandler fh = new FileHandler(conf);
fh.load(xmlStream);
Schema Schema=SchemaFactory.newInstance(xmlstants.W3C_XML_Schema_NS_URI).newSchema(新文件(“C:\\config.xsd”);
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
docBuilderFactory.setSchema(模式);
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
//如果希望在存在无效xml文档时引发异常,
//您需要设置自己的ErrorHandler,因为默认
//行为是只打印一条错误消息。
setErrorHandler(新的ErrorHandler(){
@凌驾
公共无效警告(SAXParseException异常)引发SAXParseException{
抛出异常;
}
@凌驾
公共无效错误(SAXParseException异常)引发SAXParseException{
抛出异常;
}
@凌驾
公共void fatalError(SAXParseException异常)引发SAXParseException{
抛出异常;
}
});
conf=newBasicConfigurationBuilder(XMLConfiguration.class).configure(new Parameters().xml())
.setFileName(“config.xml”)
.setDocumentBuilder(docBuilder)
.setSchemaValidation(真)
.setExpressionEngine(新的XPathExpressionEngine())
).getConfiguration();
FileHandler fh=新的FileHandler(conf);
fh.负载(xmlStream);