dale
dale

Reputation: 439

XML Invalid byte 1 of 1-byte UTF-8 sequence

I have a program that takes two xml files and merge into one, while I'm doing this, I managed to convert from "and " to "and ’". Not talking about why I'm doing this, here's the code snippet, removing "’" error no longer exist that's why I paste it here.

convertedString = replace(convertedString, (String)"and ", 
                (String)"and ’");
convertedString = replace(convertedString, (String)""", 
                (String)"\\\"");
convertedString = StringEscapeUtils.unescapeHtml(convertedString);

with printDocument method:

private static void printDocument(Document doc, OutputStream out) 
    throws IOException, TransformerException 
    {     
        TransformerFactory tf = TransformerFactory.newInstance();     
        Transformer transformer = tf.newTransformer();     
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");     
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");     
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");     
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");     
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-" +
                "amount", "4");      
        transformer.transform(new DOMSource(doc),           
                new StreamResult(new OutputStreamWriter(out, "UTF-8"))); 
    }

running my program I get

com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
    at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:684)
    at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:554)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipChar(XMLEntityScanner.java:1416)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2793)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:235)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)

Even though it might to do with UTF-8 in printDocument() method, changing it to ISO-8859-1 wasn't going to help.

So could anyone help me what the issue is? Much appreciated

Upvotes: 0

Views: 11627

Answers (2)

Ghadah
Ghadah

Reputation: 11

If you are using eclipse. try navigating to Preferences/General/Workspace. then change the change the "text file encoding" to UTF-8

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163262

The XML parser is trying to interpret the input as UTF-8 but it isn't UTF-8.

Upvotes: 0

Related Questions