gpt4 book ai didi

xsd - 使用 dom4j DOMDocument 提供 validator.validate(DOMSource) 在 java 1.6 中失败(xsi :noNamespaceSchemaLocation is not allowed), 在 1.5 中工作)

转载 作者:行者123 更新时间:2023-12-02 05:22:18 25 4
gpt4 key购买 nike

使用 dom4j DOMDocument 提供 validator.validate(DOMSource) 在 java 1.6 中失败(不允许 xsi:noNamespaceSchemaLocation 出现在根元素中),在 1.5 中有效

我发现以下问题非常棘手(好吧,这是轻描淡写的) - 任何见解将不胜感激。目前看来最好的想法是放弃 dom4j,转而使用例如XOM(http://stackoverflow.com/questions/831865/what-java-xml-library-do-you-recommend-to-replace-dom4j)。

我一直在内存中验证从 dom4j 'new DOMDocument()' 创建的 XML - 但这不适用于 Java 6。

以下对 dom4j (1.6.1) DOMDocument 派生的 DOMSource 的 validate(source) 的调用适用于 Java 1.5.x,但适用于 Java 1.6.x:

public void validate() throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setErrorHandler(null);
Schema schemaXSD = schemaFactory.newSchema(new URL(getSchemaURLString()));
Validator validator = schemaXSD.newValidator();
DOMSource source = new DOMSource(getDocument());
validator.validate(source);
}

getSchemaURLString() 还用于向根节点添加 xsi:noNamespaceSchemaLocation 属性,即:xsi:noNamespaceSchemaLocation="http://localhost:8080/integration/xsd/fqlResponseSchema-2.0.xsd"

异常(exception)情况如下:

Exception:  org.xml.sax.SAXParseException: cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'.;                
complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'specialfields'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:417)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3182)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.processAttributes(XMLSchemaValidator.java:2659)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:2066)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:705)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:273)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:240)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:186)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:104)
at javax.xml.validation.Validator.validate(Validator.java:127)

这是在禁用对 validator.validate(source) 的调用后生成的 XML 的开头:

<?xml version="1.0" encoding="utf-8"?> 
<meetings xsi:noNamespaceSchemaLocation="http://localhost:8080/integration/xsd/fqlResponseSchema-2.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
.............
</meetings>

以及 XSD:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="meetings">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" ref="summary" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="meeting" />
</xs:sequence>
<xs:element ref="error" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="summary">
................

所以我的根元素被拒绝,因为它包含 xsi:noNamespaceSchemaLocation 属性。并且架构本身没有将其指定为我的根元素的有效属性?

此时,在我看来,我需要放弃 dom4j 来完成此任务,并切换到其他解决方案之一,例如此处概述的:

但无论如何我想知道我做错了什么!

提前致谢。

最佳答案

我遇到了同样的问题,我在

找到了以下文档

http://www.ibm.com/developerworks/xml/library/x-javaxmlvalidapi/index.html

Validate against a document-specified schema

Some documents specify the schema they expect to be validated against, typically using xsi:noNamespaceSchemaLocation and/or xsi:schemaLocation attributes like this:

<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.example.com/document.xsd">
...

If you create a schema without specifying a URL, file, or source, then the Java language creates one that looks in the document being validated to find the schema it should use. For example:

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 
Schema schema = factory.newSchema();

However, normally this isn't what you want. Usually the document consumer should choose the schema, not the document producer. Furthermore, this approach works only for XSD. All other schema languages require an explicitly specified schema location.

关于xsd - 使用 dom4j DOMDocument 提供 validator.validate(DOMSource) 在 java 1.6 中失败(xsi :noNamespaceSchemaLocation is not allowed), 在 1.5 中工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5080784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com