Java 在 <import namespace> 中加载本地 xsd 文件
Java load local xsd file in <import namespace>
我尝试加载我的本地 xsd 文件(import...schemaLocation="gml.xsd"
但似乎没有任何效果,因为我总是遇到同样的错误:
org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; src-resolve: Cannot resolve the name 'gml:AbstractFeature' to a(n) 'element declaration' component.
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 document created by 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;
}
}
由于您提供的是字节流,XML 库不知道文件的位置,因此无法解析相对路径。
作为fall-back,它可能解析了相对于当前目录的相对路径,但由于文件不存在,因此找不到它们。
将 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); // 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;
}
}
优点:您不必记得关闭流,您似乎已经忘记了。
我尝试加载我的本地 xsd 文件(import...schemaLocation="gml.xsd"
但似乎没有任何效果,因为我总是遇到同样的错误:
org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 122; src-resolve: Cannot resolve the name 'gml:AbstractFeature' to a(n) 'element declaration' component.
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 document created by 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;
}
}
由于您提供的是字节流,XML 库不知道文件的位置,因此无法解析相对路径。
作为fall-back,它可能解析了相对于当前目录的相对路径,但由于文件不存在,因此找不到它们。
将 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); // 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;
}
}
优点:您不必记得关闭流,您似乎已经忘记了。