How to process xml with invalid utf-8 bytes in Go including ISO 8859 1 encodings

I recently had to process a large number of xml files that were not UTF-8 encoded.  They also happened to include characters that were outside the valid ASCII range.  I did not really want to keep these invalid characters, and this solution will work for that use case.

If you do need to keep non-ascii characters, this simple solution will not work for you.

The trick to making this work is to create a wrapper around the io.ByteReader that will pre-filter out non-ascii.  Due to how the xml Decoder works, it also expects the wrapper to implement the io.Reader interface.  So here is the code that you can use to perform this filtering.  It assumes you have the asciireader somewhere in your GOPATH and that the file you want to process is called file.xml

Here is the code for the wrapper that filters out non ascii bytes