How to fix Invalid byte 1 of 1-byte UTF-8 sequence

java, xml, xml-parsing

Solution

How to fix this issue ?

Read the data using the correct character encoding. The error message means that you are trying to read the data as UTF-8 (either deliberately or because that is the default encoding for an XML file that does not specify `<?xml version="1.0" encoding="somethingelse"?>`) but it is actually in a different encoding such as ISO-8859-1 or Windows-1252.

To be able to advise on how you should do this I'd have to see the code you're currently using to read the XML.

Problem

I am trying to fetch the below xml from db using a java method but I am getting an error Code used to parse the xml ``` DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(new ByteArrayInputStream(cond.getBytes())); Document doc = db.parse(is); Element elem = doc.getDocumentElement(); // here we expect a series of <data><name>N</name><value>V</value></data> NodeList nodes = elem.getElementsByTagName("data"); TableID jobId = new TableID(_processInstanceId); Job myJob = Job.queryByID(_clientContext, jobId, true); if (nodes.getLength() == 0) { log(Level.DEBUG, "No data found on condition XML"); } for (int i = 0; i < nodes.getLength(); i++) { // loop through the <data> in the XML Element dataTags = (Element) nodes.item(i); String name = getChildTagValue(dataTags, "name"); String value = getChildTagValue(dataTags, "value"); log(Level.INFO, "UserData/Value=" + name + "/" + value); myJob.setBulkUserData(name, value); } myJob.save(); ``` The Data ``` <ContactDetails>307896043</ContactDetails> <ContactName>307896043</ContactName> <Preferred_Completion_Date> </Preferred_Completion_Date> <service_address>A-End Address: 1ST HELIERST HELIERJT2 3XP832THE CABLES 1 POONHA LANEST HELIER JE JT2 3XP</service_address> <ServiceOrderId>315473043</ServiceOrderId> <ServiceOrderTypeId>50</ServiceOrderTypeId> <CustDesiredDate>2013-03-20T18:12:04</CustDesiredDate> <OrderId>307896043</OrderId> <CreateWho>csmuser</CreateWho> <AccountInternalId>20100333</AccountInternalId> <ServiceInternalId>20766093</ServiceInternalId> <ServiceInternalIdResets>0</ServiceInternalIdResets> <Primary_Offer_Name action='del'>MyMobile Blue &#163;44.99 [12 month term]</Primary_Offer_Name> <Disc_Reason action='del'>8</Disc_Reason> <Sup_Offer action='del'>80000257</Sup_Offer> <Service_Type action='del'>A-01-00</Service_Type> <Priority action='del'>4</Priority> <Account_Number action='del'>0</Account_Number> <Offer action='del'>80000257</Offer> <msisdn action='del'>447797142520</msisdn> <imsi action='del'>234503184</imsi> <sim action='del'>5535</sim> <ocb9_ARM action='del'>false</ocb9_ARM> <port_in_required action='del'> </port_in_required> <ocb9_mob action='del'>none</ocb9_mob> <ocb9_mob_BB action='del'> </ocb9_mob_BB> <ocb9_LandLine action='del'> </ocb9_LandLine> <ocb9_LandLine_BB action='del'> </ocb9_LandLine_BB> <Contact_2> </Contact_2> <Acc_middle_name> </Acc_middle_name> <MarketCode>7</MarketCode> <Acc_last_name>Port_OUT</Acc_last_name> <Contact_1> </Contact_1> <Acc_first_name>.</Acc_first_name> <EmaiId> </EmaiId> ``` The ERROR ``` org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence. ``` I read in some threads it's because of some special characters in the xml. How to fix this issue ?

Original source

Related problems