我尝试在java中创建XML验证器,具有架构XSD。 我的file.xml的开头
<?xml version="1.0" encoding="UTF-8"?>
<Dataset xmlns:dc="http://purl.org/dc/terms/" xmlns:dwc="http://rs.tdwg.org/dwc/terms/" xmlns:tcs="http://www.tdwg.org/schemas/tcs/1.01" xmlns:eol="http://www.eol.org/transfer/content/0.3" xmlns:gisin="http://www.gisin.org/gisin/SpeciesStatus" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="AbstractModel.xsd">
<Metadata>...
架构AbstractModel.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:plic="http://www.gbif.es/PliC/" xmlns:dc="http://purl.org/dc/terms/" xmlns:dcelem="http://purl.org/dc/elements/1.1/" xmlns:dwc="http://rs.tdwg.org/dwc/terms/" xmlns:abcd="http://www.tdwg.org/schemas/abcd/2.06" xmlns:tcs="http://www.tdwg.org/schemas/tcs/1.01" xmlns:simple="http://rs.tdwg.org/dwc/xsd/simpledarwincore/" xmlns:gisin="http://www.gisin.org/gisin/SpeciesStatus" xmlns:eol="http://www.eol.org/transfer/content/0.3" xmlns:eml="eml://ecoinformatics.org/eml-2.1.1" xmlns:sql="urn:schemas-microsoft-com:mapping-schema" xmlns:ns1="https://github.com/tdwg/dwc/tree/master/xsd">
<xs:import namespace="http://www.tdwg.org/schemas/abcd/2.06" schemaLocation="http://rs.tdwg.org/abcd/2.06/ABCD_2.06.xsd"/>
<xs:import namespace="http://www.tdwg.org/schemas/tcs/1.01" schemaLocation="http://www.tdwg.org/uploads/media/v101.xsd"/>
<xs:import namespace="http://www.gisin.org/gisin/SpeciesStatus" schemaLocation="http://www.gisin.org/GISIN/SpeciesStatus_4_0_0.xsd"/>
<xs:import namespace="http://rs.tdwg.org/dwc/terms/" schemaLocation="tdwg_dwc_extensions.xsd"/>
<xs:import namespace="eml://ecoinformatics.org/eml-2.1.1" schemaLocation="eml.xsd"/>
<xs:import namespace="http://www.eol.org/transfer/content/0.3" schemaLocation="content_0_3.xsd"/>
<xs:import namespace="http://purl.org/dc/terms/" schemaLocation="http://dublincore.org/schemas/xmls/qdc/dcterms.xsd"/>
<xs:import namespace="http://purl.org/dc/elements/1.1/" schemaLocation="http://dublincore.org/schemas/xmls/qdc/dc.xsd"/>
<xs:element name="Dataset">
<xs:annotation>.......
首先,我在Internet上关注一些示例,使用Java API for XML创建一个简单的验证器类
public static void validate(String xsdPath, String xmlPath){
try{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));
} catch (IOException | SAXException e){
System.out.println("Exception: "+e.getMessage());
}
}
我读过,因为读取导入的XSD是必要的,使用LSResourceResolver和LSInput,但是我不知道如何使用几个XSD导入实现这些类。可以使用Java进行这种验证吗?使用某些库比较方便吗?
我不知道如何在我的特定问题中使用可能的答案:Validate an XML against an XSD in Java / Getting a hold of the schemaLocation。例如,我有一个实现LSResourceResolver的类ResourceResolver,但是根据答案,这个类的构造函数需要两个参数public ResourceResolver(URL base, Map<URI, String> nsmap)
我不知道从哪个类或变量,我应该传递这些值。 / p>