【问题标题】:Java validation by xsd with xmlns通过 xsd 使用 xmlns 进行 Java 验证
【发布时间】:2017-05-31 14:04:39
【问题描述】:

我需要通过 XSD 验证 XML。如果我使用没有命名空间的 xsd 验证是好的。但是如果 XSD 有 targetNamespace 我会出错。

文本错误:cvc-elt.1:找不到元素“RequestGKN”的声明。

XML

<?xml version="1.0" encoding="utf-8" ?>
<RequestGKN 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="urn://x-artefacts-rosreestr-gov-ru/requests/gkn/3.0.9">   
    <title>123</title>
</RequestGKN>

XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="urn://x-artefacts-rosreestr-gov-ru/requests/gkn/3.0.9"
        elementFormDefault="qualified">
    <xs:element name="RequestGKN">
        <xs:annotation>
            <xs:documentation>root element</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Title" type="xs:string">
                    <xs:annotation>
                        <xs:documentation>info</xs:documentation>
                    </xs:annotation>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Java 代码

    Schema schema = null;
    Document document = null;

    xml = "<?xml version=\"1.0\"  encoding=\"utf-8\" ?>\n" +
            "<RequestGKN xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"urn://x-artefacts-rosreestr-gov-ru/requests/gkn/3.0.9\">" +
            "<title>123</title>" +
            "</RequestGKN>";
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder parser = builderFactory.newDocumentBuilder();
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        document = builder.parse(new InputSource(new StringReader(xml)));
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        path = locationXSD.substring(0, locationXSD.lastIndexOf('/')+1);

        //factory.setResourceResolver(new ResourceResolver());
        Source schemaFile = new StreamSource(TemplateHandler.class.getClassLoader().getResourceAsStream(locationXSD));
        schema = factory.newSchema(schemaFile);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));

【问题讨论】:

    标签: java xml validation xsd


    【解决方案1】:

    在创建DocumentBuilder 之前,请确保schema 已在DocumentBuilderFactory 中设置。 schema 对象是线程安全的,可以毫无问题地重复使用。

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    String path = locationXSD.substring(0, locationXSD.lastIndexOf('/')+1);
    Source schemaFile = new StreamSource(TemplateHandler.class.getClassLoader().getResourceAsStream(locationXSD));
    schema = factory.newSchema(schemaFile);//thread safe object which can be reused
    
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    builderFactory.setSchema(schema);
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    document = builder.parse(new InputSource(new StringReader(xml)));
    
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(document));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2011-03-10
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多