【发布时间】:2012-02-07 04:21:42
【问题描述】:
我正在使用以下代码根据指定的 XML 架构验证 XML 文档 (.gpx)。我在本地将架构存储为 .xsd 文件。问题在于,此方法使用 Internet 连接来验证架构。有没有办法在不使用互联网连接的情况下做到这一点? (考虑到我在本地存储 XML 模式)。
代码:
public static boolean validate(String XmlDocumentUrl, String SchemaUrl) {
SAXParser parser = new SAXParser();
try {
parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature(
"http://apache.org/xml/features/validation/schema", true);
parser.setFeature(
"http://apache.org/xml/features/validation/schema-full-checking",
false);
parser.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
SchemaUrl);
Validator handler = new Validator();
parser.setErrorHandler(handler);
parser.parse(XmlDocumentUrl);
if (handler.validationError == true){
System.out.println("XML Document has Error:"
+ handler.validationError + ""
+ handler.saxParseException.getMessage());
return false;
}
else{
System.out.println("XML Document is valid");
return true;
}
} catch (java.io.IOException ioe) {
System.out.println("IOException" + ioe.getMessage());
} catch (SAXException e) {
System.out.println("SAXException" + e.getMessage());
}
return false;
}
感谢和问候,
佩塔
【问题讨论】:
标签: java xsd validation offline gpx