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

。_饼干妹妹 提交于 2019-12-11 17:51:59

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/12212729/xml-invalid-byte-1-of-1-byte-utf-8-sequence

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!