I am using org.geotools version 2.7.0 and I am trying to get some shapefiles to GML 3.1.1. Sadly I got an exception:

java.lang.RuntimeException: Encode failed for _Feature. Cause: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 
at org.geotools.xml.impl.ElementEncodeExecutor.visit(ElementEncodeExecutor.java:132)
at org.geotools.xml.impl.BindingWalker$BindingExecutionChain.execute(BindingWalker.java:215)
at org.geotools.xml.impl.BindingWalker.walk(BindingWalker.java:181)
at org.geotools.xml.impl.BindingVisitorDispatch.walk(BindingVisitorDispatch.java:44)
at org.geotools.xml.impl.ElementEncoder.encode(ElementEncoder.java:83)
at org.geotools.xml.Encoder.encode(Encoder.java:1100)
at org.geotools.xml.Encoder.encode(Encoder.java:836)
at org.geotools.xml.Encoder.encode(Encoder.java:564)

Maybe it's a space or an accented char (shape's attributes are full of accented chars) but I am very new to geotools and GIS, this error is completely blocking me. I can't find a way to specify a charset to read data from shape (by set up DataStore) or to write data into GML file (by set up Encoder). What am I missing?

I am trying to remove not allowed symbols from attributes and element names by using

StringBuilder sb = new StringBuilder();
if (!s.substring(0, 1).matches("[a-zA-Z_]"))
    throw new IllegalArgumentException();
sb.append(s.substring(0, 1));
for (int i = 1; i < s.length(); i++) {
    if (s.substring(i, i + 1).matches("[a-zA-Z0-9_.-]")) {
    sb.append(s.substring(i, i + 1));
return sb.toString();

to sanitize shape feature attribute's values. Now attributes looks good but I am still getting the same exception.

I am new to GIS and org.geotools.

  • Current GeoTools release is 15.1 - so you might want to try a newer version
    – Ian Turton
    Commented Sep 1, 2016 at 14:50
  • 2
    List your attributes. Attribute names in GML are of type NCName which means that everything is not acceptable stackoverflow.com/questions/1631396/….
    – user30184
    Commented Sep 1, 2016 at 15:01
  • @iant Hello, I can't upgrade right now. Do you think it's an already solved issue?
    – Francesco
    Commented Sep 1, 2016 at 15:12
  • @Franz - who knows that's 8 full releases old, you could check the release notes for each one, start at geotoolsnews.blogspot.co.uk/2011/03/geotools-270-released.html and work forward
    – Ian Turton
    Commented Sep 1, 2016 at 15:15
  • @iant maybe the issue happens when shapefile has spaces in its name?
    – Francesco
    Commented Sep 5, 2016 at 9:00

1 Answer 1


Ok, I did it. I have to check for every SimpleFeatureTypeImpl attribute who is an instance of AttributeDescriptorImpl. I check if it's name's local part and id are valid NCNames values. I replaced every non NCName entry with a NCName valid one. To check if a String is a NCName one I used org.eclipse.emf.ecore.xml.type.internal.DataValue.XMLChar.isValidNCName(). This prevents error due to spaces in shapefile's name.

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.