Wednesday, July 24, 2013

Blank browser page - Error: The character encoding of the plain text document was not declared

I was browsing along fine on our financial calculator pages at www.calcxml.com using Firefox 22.0. All of a sudden, the calculator input pages stopped rendering. Just a blank page would be displayed. Here is the error message I found in the Firefox error console:


Error: The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature.

After googling the error and going through several suggestions, nothing seemed to help. I was already declaring the character encoding for the web pages. Long story short, after much troubleshooting, I figured out it was the cookie length. Each of our calculators can store a cookie with the previous values used as input. I had gone through about 20 of them when the error finally occurred. I'm not sure what cookie limitations there are, but the error appeared to occur somewhere between 7600 and 7800 characters. (The error occurred with 7786 characters.)

Saturday, February 09, 2013

org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence

I came across this error as I was trying to unmarshal some xml data that was being received by a REST service:

org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence

I would only get the error sporadically. I finally determined it was due to certain characters in the XML being passed in. When I changed the XML encoding of the incoming XML to ISO-8859-1, then one particular XML document would work fine, but I didn't want to have to worry about changing from UTF-8, as UTF-8 is pretty much the global standard now.

After digging a little deeper, I decided to change my unmarshal code from this:

            byte[] bArray = xmlInput.getBytes();
           ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
            JAXBContext jc = JAXBContext.newInstance(Article.class);
            Unmarshaller u = jc.createUnmarshaller();
            Article article = (Article) u.unmarshal(bais);


to this:

            StringReader reader = new StringReader(xmlInput);
            JAXBContext jc = JAXBContext.newInstance(Article.class);
            Unmarshaller u = jc.createUnmarshaller();
            Article article = (Article) u.unmarshal(reader);


This solved my problems! Apparently the conversion of the xml into a byte array was causing the problem. My initial code was taken from a tutorial I had gone through so I hadn't considered that it would be causing the problems but glad I found it.