Geographic Information Systems Asked by justin1298 on February 15, 2021
I would like to control if my GML file is valid with my actual XSD using GeoTools.
I found something here: GML2 Parsing TestSchema Example. But I don’t know if it’s a good way to do what I want.
Configuration configuration = new org.geotools.gml3.GMLConfiguration();
Parser parser = new org.geotools.xsd.Parser(configuration);
//the xml instance document above
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream xsd = classloader.getResourceAsStream("gml/config.xsd");
int i =0;
//parse
SimpleFeatureCollection fc = (SimpleFeatureCollection) parser.parse(xsd);
fc.accepts( new AbstractFeatureVisitor(){
public void visit( Feature feature ) {
SimpleFeature f = (Feature) i.next();
Point point = (Point) f.getDefaultGeometry();
String name = (String) f.getAttribute( "name" );
}
}, new NullProgressListener() );
In my code I don’t know where I can specify my GML file for validation.
You don't need to use a GML aware parser to check for validity a normal Java XML Validator
will do. So something like:
public boolean validate(File gml, URL schemaURL) {
Source xmlFile = new StreamSource(gml);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
Schema schema = schemaFactory.newSchema(schemaURL);
Validator validator = schema.newValidator();
validator.validate(xmlFile);
System.out.println(xmlFile.getSystemId() + " is valid");
return true;
} catch (SAXException e) {
System.out.println(xmlFile.getSystemId() + " is NOT valid reason:" + e);
} catch (IOException e) {
}
return false;
}
Answered by Ian Turton on February 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP