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.


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, privacy policy and cookie policy

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