简体   繁体   English

java xml验证JDK 1.5与JDK 1.6的区别

[英]java xml validation JDK 1.5 JDK 1.6 difference

I have a problem with Java xml validation. 我对Java xml验证有疑问。

I have the following xsd: 我有以下xsd:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="TEST">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="LAST_NAME">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:minLength value="1" />
              <xsd:maxLength value="30" />
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:element>
        <xsd:element name="FIRST_NAME">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:minLength value="1" />
              <xsd:maxLength value="20" />
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:element>
        <xsd:element name="DOB" nillable="true" type="xsd:date" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

and xml: 和xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <LAST_NAME>Lastname</LAST_NAME>
  <FIRST_NAME>Firstname</FIRST_NAME>
  <DOB xsi:nil="true"/>
</TEST>

The (simplified) code of my validator: 我的验证器的(简化)代码:

boolean valid=true;
try {
    Source schemaSource = new StreamSource(xsdInputStream);
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = parser.parse(xmlInputStream);
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = factory.newSchema(schemaSource);

    Validator validator = schema.newValidator();
    try {
        validator.validate(new DOMSource(document));
    } catch (SAXException e) {
        logger.log(Level.INFO, e.getMessage(), e);
        valid = false;
    }

} catch( Exception ex ) {
    logger.log(Level.SEVERE, ex.getMessage(), ex);
    valid=false;
}

The testprogram has a different behavior in JDK 1.5 and JDK 1.6. 在JDK 1.5和JDK 1.6中,testprogram的行为不同。 The xml is valid in JDK 1.5 but invalid in JDK 1.6. xml在JDK 1.5中有效,但在JDK 1.6中无效。 The error message is the following: 错误消息如下:

Element 'DOB' is a simple type, so it cannot have attributes, excepting those whose namespace name is identical to 'http://www.w3.org/2001/XMLSchema-instance' and whose [local name] is one of 'type', 'nil', 'schemaLocation' or 'noNamespaceSchemaLocation'. However, the attribute, 'xsi:nil' was found.

Which JDK is correct? 哪个JDK是正确的? How to change the xml/xsd to be valid in both? 如何将xml / xsd更改为两者均有效?

Try putting attributeFormDefault="qualified" in your XSD. 尝试在XSD中放入attributeFormDefault =“ qualified”。 That shouldn't make a difference, but it's a quick test. 那不应该有什么不同,但这是一个快速的测试。

Also: you don't set your DocumentBuilder to be namespace-aware. 另外:您不会将DocumentBuilder设置为可识别名称空间。 That would certainly break the validation, but it would break under 1.5 as well as 1.6. 那肯定会破坏验证,但是会跌破1.5和1.6。

And as a general comment, validation at the time of parsing is more useful, as you can see the line numbers of the content that failed validation. 作为一般性评论,解析时的验证更为有用,因为您可以看到验证失败的内容的行号。 Here's the code to do it ( schema is created previously): 这是执行此操作的代码( schema是先前创建的):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(false);
dbf.setSchema(schema);
DocumentBuilder db = dbf.newDocumentBuilder();

I would say this is a bug in Java 6. You can always put xsi attributes in any element. 我会说这是Java 6中的错误。您始终可以将xsi属性放在任何元素中。

It's very similar to this bug, 与这个错误非常相似,

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6790700 http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6790700

Try the fix 6u14. 尝试修复6u14。 It most likely will fix yours too. 它很可能也会修复您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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