[英]Java Validate XML using XSD with no namespaces
我正在嘗試驗證以下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'
解決方案(感謝@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;
}
}
而這個方法來獲取Schema:
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();
}
}
}
沒關系 :最奇怪的是:適用於在Windows 7上運行的Tomcat 6; 不能在Linux上運行jboss ...
這可能是一個很長的鏡頭,但我記得當我們使用JBoss 5.0或.1時,我們在使用的xerces實現中發現了一個嚴重的錯誤。 請檢查版本是否有錯誤或只是嘗試更新版本的xerces庫。
您的實例文檔和架構文檔看起來很好; 你調用驗證的方式有問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.