Java在中加载本地XSD文件

huangapple go评论67阅读模式
英文:

Java load local xsd file in <import namespace>

问题

我尝试加载本地的 xsd 文件(import...schemaLocation="gml.xsd"),但似乎无法正常工作,因为我始终得到相同的错误:

org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; src-resolve: 无法将名称 'gml:AbstractFeature' 解析为 'element declaration' 组件。

config.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:pcrs="http://cnig.gouv.fr/pcrs" xmlns:pcrs-i="http://cnig.gouv.fr/pcrs-information" targetNamespace="http://cnig.gouv.fr/pcrs" elementFormDefault="qualified" version="2.0beta2">
	<import namespace="http://cnig.gouv.fr/pcrs-information" schemaLocation="CNIG_PCRS-INFO_v2.0.xsd"/>
	<import namespace="http://www.opengis.net/gml/3.2" schemaLocation="gml.xsd"/>
	<!--XML Schema 文档由 ShapeChange 创建 - http://shapechange.net/-->
	<element name="AffleurantEnveloppePCRS" type="pcrs:AffleurantEnveloppePCRSType" substitutionGroup="gml:AbstractFeature">

java:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream xsd = classloader.getResourceAsStream("gml/config.xsd");
InputStream gml = classloader.getResourceAsStream("gml/test.gml");

public boolean validateXSD() {
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(gml));
        return true;
    } catch (Exception error) {
        System.out.println(error);
        return false;
    }
}
英文:

I try to load my local xsd file (import...schemaLocation=&quot;gml.xsd&quot; but it seems nothing working because I got always same error :

org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; src-resolve: Cannot resolve the name &#39;gml:AbstractFeature&#39; to a(n) &#39;element declaration&#39; component.

config.xsd :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;schema xmlns=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:gml=&quot;http://www.opengis.net/gml/3.2&quot; xmlns:pcrs=&quot;http://cnig.gouv.fr/pcrs&quot; xmlns:pcrs-i=&quot;http://cnig.gouv.fr/pcrs-information&quot; targetNamespace=&quot;http://cnig.gouv.fr/pcrs&quot; elementFormDefault=&quot;qualified&quot; version=&quot;2.0beta2&quot;&gt;
	&lt;import namespace=&quot;http://cnig.gouv.fr/pcrs-information&quot; schemaLocation=&quot;CNIG_PCRS-INFO_v2.0.xsd&quot;/&gt;
	&lt;import namespace=&quot;http://www.opengis.net/gml/3.2&quot; schemaLocation=&quot;gml.xsd&quot;/&gt;
	&lt;!--XML Schema document created by ShapeChange - http://shapechange.net/--&gt;
	&lt;element name=&quot;AffleurantEnveloppePCRS&quot; type=&quot;pcrs:AffleurantEnveloppePCRSType&quot; substitutionGroup=&quot;gml:AbstractFeature&quot;&gt;

java :

    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    InputStream xsd = classloader.getResourceAsStream(&quot;gml/config.xsd&quot;);
    InputStream gml = classloader.getResourceAsStream(&quot;gml/test.gml&quot;);

    public boolean validateXSD() {
            try {
                SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = factory.newSchema(new StreamSource(xsd));
                Validator validator = schema.newValidator();
                validator.validate(new StreamSource(gml));
                return true;
            } catch (Exception error) {
                System.out.println(error);
                return false;
            }
        }

答案1

得分: 3

因为您提供了一个字节流,XML库不知道文件的位置,所以无法解析相对路径。

作为一种后备措施,它可能会将相对路径解析为相对于当前目录的路径,但由于文件不在那里,因此找不到它们。

请将XSD指定为URI。

public boolean validateXSD() {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    URL xsd = classloader.getResource("gml/config.xsd");
    URL gml = classloader.getResource("gml/test.gml");
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(xsd); // 将URL传递给XML库
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(gml.toString())); // 将URL作为字符串传递
        return true;
    } catch (Exception error) {
        System.out.println(error);
        return false;
    }
}

优势: 您不必记得关闭流,这似乎是您忘记的部分。

英文:

Since you're supplying a byte stream, the XML library has no idea where the file is located, so it cannot resolved relative paths.

As a fall-back, it likely resolved the relative paths relative to the current directory, but since the files are not there, they are not found.

Specify the XSD as a URI.

public boolean validateXSD() {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    URL xsd = classloader.getResource(&quot;gml/config.xsd&quot;);
    URL gml = classloader.getResource(&quot;gml/test.gml&quot;);
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(xsd); // Pass URL to XML library
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(gml.toString())); // Pass URL as a string
        return true;
    } catch (Exception error) {
        System.out.println(error);
        return false;
    }
}

Advantage: You don't have to remember to close the streams, which you seemed to have forgot.

huangapple
  • 本文由 发表于 2020年9月18日 05:53:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63946616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定