• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unwanted characters while converting data from blob to string  RSS feed

 
Ranch Hand
Posts: 1085
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

i have some xml data which is stored in db(Oracle) in blob format.
when i am fetching that data and converting that into string i am getting some junk characters preceding to my data.
some of those characters are : "��"
unfortunately i am not able to paste all those characters here.

code, i am using to convert blob to string:



i did some googling and found some code:




but it also didn't work, it is replacing my all data to blank.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blobs aren't Strings.
Have you worked out what those characters are? You can print their values in hexadecimal and compare them with Unicode charts and see what they represent.
Also, is there any evidence of an encoding?
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This surely can't be right!

If you look at the specification for interface Blob you will see that the two parameters to getBytes() are the offset and length to get so you can only get blob.length())-1 bytes starting from position 1! Also, why are you ignoring the first byte?

In your position I would print the hex value of the first 10 or so bytes before going any further.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it says in the String class not to use that constructor.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:it says in the String class not to use that constructor.


When did this happen? It is not deprecated in my 1.7 Javadoc !
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite often when you see what looks like junk preceding an XML document, what you are seeing is actually a Byte order mark.

And yes, you shouldn't use "new String(xmlBytes)" to convert an array of bytes which contain XML to a String, because it may use an encoding which differs from the encoding which is actually declared by the XML document. you should look for a solution which doesn't require converting the data to a String. If you're going to parse the data then pass the bytes directly to the parser. It's even possible that the parser may be able to deal with the byte order mark.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote: . . . When did this happen? It is not deprecated in my 1.7 Javadoc !
I have read the wrong constructor. Sorry. I saw byte[] int and thought that was the constructor arguments.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!