Java使用不带名称空间的XSD验证XML
我正在尝试验证以下XML
<query>
<colors logic="AND">
<color main="BLUE" tone="DARK" operator="=" />
</colors>
</query>
使用以下XSD
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="color">
<xsd:attribute name="main" type="xsd:string" use="required"/>
<xsd:attribute name="tone" type="xsd:string" use="required"/>
<xsd:attribute name="operator" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="colors">
<xsd:sequence>
<xsd:element name="color" type="color" maxOccurs="unbounded"> </xsd:element>
</xsd:sequence>
<xsd:attribute name="logic" type="xsd:string" use="required"/>
</xsd:complexType>
<xsd:complexType name="query">
<xsd:sequence>
<xsd:element name="colors" type="colors" maxOccurs="2"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="query" type="query"></xsd:element>
</xsd:schema>
所以。。。我想验证没有任何名称空间的XML。我不能更改XML,因为它是由另一个应用程序生成的,我只想在服务器端保证客户端发送了正确的请求
当我尝试根据XSD验证XML时,会收到以下异常消息:
cvc-elt.1: Cannot find the declaration of element 'query'
我已经搜索并找到了类似this和this的解决方案,但没有成功
解决方案(感谢@Traroth为我指明了正确的方向)--
下面是我如何验证它的:
我有这个功能:
public static Document buildValidRequest(String content, Schema xsd) throws SAXParseException, SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setSchema(xsd);
XMLSimpleErrorHandler errorHandler = new XMLSimpleErrorHandler();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(errorHandler);
StringReader reader = new StringReader(content);
Document validXML = builder.parse(new InputSource(reader));
if (errorHandler.getException() != null) {
throw errorHandler.getException();
}
return validXML;
}
和该类来处理错误:
public class XMLSimpleErrorHandler implements ErrorHandler {
private SAXParseException exception;
@Override
public void warning(SAXParseException e) {
this.exception = e;
}
@Override
public void error(SAXParseException e) {
this.exception = e;
}
@Override
public void fatalError(SAXParseException e) {
this.exception = e;
}
public SAXParseException getException() {
return exception;
}
}
此方法用于获取模式:
private static Schema getSchema(String xsdPath) throws SAXException, IOException {
InputStream resourceAsStream = null;
try {
ServiceManager.getInstance().getLoggerManager().debug(RESTInitServlet.LOGCONTEXT, TAG, "Retrieving schema: "+xsdPath);
resourceAsStream = getInstance().getClass().getResourceAsStream(xsdPath);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaSource = new StreamSource(resourceAsStream);
Schema schema = schemaFactory.newSchema(schemaSource);
return schema;
} finally {
if (resourceAsStream != null) {
resourceAsStream.close();
}
}
}
别担心这件事:最奇怪的事:在Windows7上运行的Tomcat6上工作;无法在Linux上运行的jboss中工作
# 1 楼答案
这可能是一个漫长的过程, 但我记得,当我们使用JBoss5.0或.1时,我们在使用的xerces实现中发现了一个严重的错误。 请检查该版本是否存在错误,或者尝试使用xerces库的更新版本
# 2 楼答案
您的实例文档和模式文档看起来不错;您调用验证的方式有问题
# 3 楼答案
你应该在这里找到答案:http://www.edankert.com/validate.html#Validate_using_external_Schema