user2942715
user2942715

Reputation: 19

Error: Byte "239" is not a member of the (7-bit) ASCII character set

I want to read a xml file in netbeans.I use this code :

DocumentBuilderFactory dbf ; DocumentBuilder db; Document doc = null;
  File file = new File(xml_url);
  dbf = DocumentBuilderFactory.newInstance();
  db = dbf.newDocumentBuilder();
  doc = db.parse(file);

and then it gives this error : Byte "239" is not a member of the (7-bit) ASCII character set.

xml header is :

<?xml version="1.0" encoding="us-ascii"?>

How can I fix this? Thanks.

Upvotes: 1

Views: 4234

Answers (2)

Liang Zhong
Liang Zhong

Reputation: 1

I would have commented under Christoffer's post, but the stackoverflow asks for 50 reputation which I don't have. I am using macOS Big Sur. The vi comes with doesn't recognize byte 239, while pico does.

Upvotes: 0

Your XML file is claiming to be ASCII in the XML header, but it's actually not, since it apparently contains non-ASCII bytes.

Fixes in order of preference:

  1. You should go to whatever/whoever generated the XML file, and get it/them to generate a new correct XML file.

  2. Byte 239 or 0xEF hex, is actually the first byte of the (redundant) UTF-8 byte order mark (BOM) 0xEF 0xBB 0xBF. I strongly suspect some software incorrectly added it to the beginning of the XML file. Notepad does, for example.

    If so, remove the byte order mark, it's the first three bytes of the file, just before <?xml. If your editor doesn't show it (e.g. i think Notepad doesn't, and Netbeans might also have problems), find another that will.

    Just opening and resaving the file with an editor that doesn't support UTF-8 BOMs might be enough.

  3. Another way to fix it would be to replace <?xml version="1.0" encoding="us-ascii"?> with <?xml version="1.0" encoding="UTF-8"?>. This will only work if my assumption about the BOM is correct.

Upvotes: 6

Related Questions