简体   繁体   English

使用xsd作为源的XML验证

[英]XML validation with xsd as Source

I want to validate my xml with xsd sxheme. 我想用xsd sxheme验证我的xml。 When I load xsd as new File() - al is ok. 当我将xsd加载为新File()时-一切正常。 But I can't do this 'xos of .jar. 但是我无法执行.jar的“ xos”。 So i use this code to load xsd 所以我用这段代码加载xsd

public static boolean init() {
    try (InputStream isSchema = Thread.currentThread().getContextClassLoader().getResourceAsStream(XSD_PATH)) {
        System.out.println(getStringFromInputStream(isSchema));
        schemaSource = new StreamSource(isSchema);
    }
    catch (IOException e) {
        logger.debug(e.toString());
        return false;
    }

    return true;
}

isScheme object has valid content of .xsd file. isScheme对象包含.xsd文件的有效内容。 But when I call it 但是当我叫它

    private boolean validateXML(File xmlFile) {
    try (InputStream stream = new FileInputStream(xmlFile)) {
        Source xmlSource = new StreamSource(stream);
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            .newSchema(schemaSource)
            .newValidator()
            .validate(xmlSource);
    }
    catch (Exception e) {
        logger.debug("ERROR XML parsing for [" + xmlFile.getName() + "] : " + e.toString());
        return false;
    }

    return true;
}

An error occurs 发生错误

DEBUG Task:82 - ERROR XML parsing for [test good 4 - копия - копия (380) - копия - копия - копия - копия - копия - копия.xml] : org.xml.sax.SAXParseException; 调试任务:82-错误的[测试良好4-копия-копия(380)-копия-копия-копия-копия-копия-копия.xml]的XML解析:org.xml.sax.SAXParseException; schema_reference.4: Failed to read schema document 'null', because 1) could not find the document; schema_reference.4:无法读取架构文档'null',因为1)找不到文档; 2) the document could not be read; 2)无法读取文件; 3) the root element of the document is not . 3)文档的根元素不是。

This is my XSD file 这是我的XSD文件

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Entry">
    <xs:complexType>
        <xs:all>
            <xs:element name="content">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:maxLength value="1024" />
                        <xs:minLength value="0" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="creationDate">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:length value="19" />
                        <xs:pattern value="[1-2][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]"></xs:pattern>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:all>
    </xs:complexType>
</xs:element>
</xsd:schema>

Example of xml xml的例子

  <?xml version="1.0" encoding="UTF-8"?>
  <Entry>
    <content>any message</content>
    <creationDate>2016-06-15 20:20:20</creationDate>
  </Entry>

Help me please. 请帮帮我。

I change mu code to this 我将mu代码更改为此

    try (InputStream stream = new FileInputStream(xmlFile)) {
        isSchema = Thread.currentThread().getContextClassLoader().getResourceAsStream(XSD_PATH);
        schemaSource = new StreamSource(isSchema);
        Source xmlSource = new StreamSource(stream);
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            .newSchema(schemaSource)
            .newValidator()
            .validate(xmlSource);
    }

and to this 并为此

try (InputStream stream = new FileInputStream(xmlFile)) {
        InputStream isSchema = Thread.currentThread().getContextClassLoader().getResourceAsStream(XSD_PATH);
        Source schemaSource = new StreamSource(isSchema);
        Source xmlSource = new StreamSource(stream);
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            .newSchema(schemaSource)
            .newValidator()
            .validate(xmlSource);
    }

But the same error. 但是同样的错误。

Looking at the source of StreamSource here : 这里查看 StreamSource的来源:

The StreamSource constructor which takes InputStream merely calls the setter. 使用InputStream的StreamSource构造函数仅调用setter。 So the underlying inputstream must be kept open to be able to read from the streamSource. 因此,基础输入流必须保持打开状态,以便能够从streamSource读取。

When the try within your init() completes, it closes the inputstream, so I the streamsource instance ( schemaSource ) becomes useless at that point. init()内的try完成时,它关闭了inputstream,因此流源实例( schemaSource )在那时变得无用。 You will have to open the schema file at the time of validating the XML. 您必须在验证XML时打开模式文件。

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

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