10

I am getting following exception while parsing the xml.

Fatal error at line -1  Invalid character '&#x0' encountered. No stack trace

I have Xml data in string format and I am parsing it using DOM parser. I am parsing data which is a response from Java server to a Blackberry client. I also tried parsing with SAX parser,but problem is not resolved. Please help.

iOSDev
  • 3,617
  • 10
  • 51
  • 91

4 Answers4

16

You have a null character in your character stream, i.e. char(0) which is not valid in an XML-document. If this is not present in the original string, then it is most likely a character decoding issue.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
6

I got the solution,

I just trimmed it with trim() and it worked perfectly fine with me.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
iOSDev
  • 3,617
  • 10
  • 51
  • 91
  • 2
    Yes, maybe that's 'cause the "null" char was at the end of your String. But I think that the real solucion should be using Replace of the char "\0", and change it for an empty-string. – Dario Quintana Jul 18 '12 at 20:33
4

Your code currently calls getBytes() using the platform default encoding - that's very rarely a good idea. Find out what the encoding of the data really is, and use that. (It's likely to be UTF-8.)

If the Blackberry includes DocumentBuilder.parse(InputSource), that would be preferable:

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
StringReader reader = new StringReader(xmlData);
try {
    Document doc = docBuilder.parse(xml); 
    doc.getDocumentElement().normalize();
} finally {
    reader.close();
}

If that doesn't work, have a very close look at your string, e.g. like this:

for (int i=0; i < xmlData.length(); i++) {
    // Use whatever logging you have on the Blackberry
    System.out.println((int) xmlData.charAt(i));
}

It's possible that the problem is reading the response from the server - if you're reading it badly, you could have Unicode nulls (\u0000) in your string, which may not appear obviously in log/debug output, but would cause the error you've shown.

EDIT: I've just seen that you're getting the base64 data in the first place - so why convert it to a string and then back to bytes? Just decode the base64 to a byte array and then use that as the basis of your ByteArrayInputStream. Then you never have to deal with a text encoding in the first place.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Also,how do I check that I have \u0000 in the string? I am new to xml handling,Is there any way to chk.? Please help. – iOSDev Jun 09 '09 at 06:14
  • 2
    Look at the second code snippet - it shows how to examine the string manually. You can use xmlData.indexOf("\u0000") to check programmatically. – Jon Skeet Jun 09 '09 at 08:30
  • Which solution? And have you examined the string? It would help if you'd give a lot more details in the question. – Jon Skeet Jun 09 '09 at 10:04
  • Hi Jon, I tried the code snippet above using StringReader. I also tested by without converting xmlDAta from byte[] to string. Im still getting same error. – iOSDev Jun 09 '09 at 10:18
  • 2
    So it sounds like you've got bad data. If you could post the base64 for the smallest response you can generate, that would help a lot. – Jon Skeet Jun 09 '09 at 10:30
0
InputStream xml = new ByteArrayInputStream(xmlData.getBytes());
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(xml);
doc.getDocumentElement().normalize();
xml.close();

Above is the code I am using for parsing.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
iOSDev
  • 3,617
  • 10
  • 51
  • 91