0
/*filePath: Its a <?xml version = "1.0" encoding="Windows-1252" standalone="yes"?>*/

//My Block of Code

String content = new String(Files.readAllBytes(Paths.get(filePath)));
content.replace('\u0002',' ');
Document doc = dBuilder.parse(new InputSource(new ByteArrayInputStream(content.getBytes("Windows-1252")))); // Getting fatal error at this point because that unicode haven't replaced.
doc.getDocumentElement().normalize();



[Fatal Error] :40799:8: An invalid XML character (Unicode: 0x2) was found in the element content of the document.

Tried with different solutions but no luck. Thanks in advance.

Murli
  • 9
  • 5
  • 2
    `content.replace('\u0002',' ');` doesn't do what you want, `content = content.replace('\u0002',' ');` will replace 0x2. –  Mar 01 '18 at 07:52
  • The more interesting question is why there is a 0x2 in the file in the first place. Better to fix the root cause than to work around it. – Henry Mar 01 '18 at 07:58
  • Yes, it is not replacing. That is the problem. – Murli Mar 01 '18 at 07:59
  • What @devpuh wrote is you have to use `content=content.replace(...)` – Henry Mar 01 '18 at 08:01
  • I don't have accessibility/authorization to alter the actual file. – Murli Mar 01 '18 at 08:01
  • Oh sorry for my blunder mistake. Thanks, Devpuh and Heney. it worked ;) – Murli Mar 01 '18 at 08:04
  • You should also better use `new String(Files.readAllBytes(Paths.get(filePath)), "Windows-1252");` to avoid encoding problems. –  Mar 01 '18 at 08:05

0 Answers0