views:

1694

answers:

6

Hi, 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.

+2  A: 

Smells like you're trying to parse an empty string as XML. Have you verified that the client is being given a good response?

Andrew
Yes,I have printed the reponse coming from the server.Its not null.Whenever I "hard code" the string response,parser works just fine.But I can't hard code response in my real project.Please help.
imMobile
Let's clear this up -- if you hard code the response your client is fine, but when you use the response from the server you're getting this error? Is that what you're saying?
Andrew
Yes, you are right.Hardcoding works fine...
imMobile
A: 
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.

imMobile
Thanks for well-formed edit of code
imMobile
+1  A: 

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
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.
imMobile
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
Which solution? And have you examined the string? It would help if you'd give a lot more details in the question.
Jon Skeet
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.
imMobile
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
+1  A: 

What character encoding are you using? Could it be that Unicode is used and the BOM is sent at the beginning?

kgiannakakis
The response coming from server is Base64 encoded,which I decode on client side.
imMobile
+3  A: 

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
A: 

Hey All,

I got the solution,

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

:-)

Thanks a lot to you all,for your solutions,which guided me towards solution.

Thanks again. itsteju

imMobile