英文:
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="gml.xsd"
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 '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;
}
}
答案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("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;
}
}
Advantage: You don't have to remember to close the streams, which you seemed to have forgot.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论