2

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.

1 Answer 1

0

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;
  }
2
  • Yes but actually I have proxy and when I use this method I have the same error.
    – justin1298
    Commented Oct 2, 2020 at 7:38
  • You can configure a proxy with the VM arguments -Dhttp.proxyHost=<proxy.host.com> -Dhttp.proxyPort=8080
    – Ian Turton
    Commented Oct 2, 2020 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.