繁体   English   中英

Java使用没有名称空间的XSD验证XML

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM