Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javascript escape() function to handle an XML containing international characters

 
Tiya Khambadkone
Ranch Hand
Posts: 110
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JavaScript escape function in my code. I pass a XML to this escape function. My XML has international characters. eg. <lastName>WÁtson</lastName>.
escape does not work properly with international characters. How should I handle those ?
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Kd wrote:escape does not work properly with international characters.


What makes you say that? As far as I can see, the Javascript escape() function is defined to work with all Unicode characters.

My Javascript reference also goes on to mention that it's a deprecated function and that you should use encodeURI() or encodeURIComponent() instead. You might consider doing that, to see if your undescribed problem goes away.
 
Tiya Khambadkone
Ranch Hand
Posts: 110
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am encoding the XML using escape function and UTF-8 and passing it over. The receiver's end when they try to decode it, get a marshalling error. I do not have much stuff to describe because at my side I am good. But have to solve the problem.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem will not get solved by keeping secrets. You've provided no information to help solve this problem except "an error is happening".

Here's what you should have already provided in order to solve this issue:
  • The exact (exact!) wording of the error message.
  • The value before encoding.
  • The value after encoding.
  • What you think should have happened that did not.
  •  
    Tiya Khambadkone
    Ranch Hand
    Posts: 110
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault, I am not trying to keep the error message secret. As I said earlier I do not have much information about the error message because it is not happening at my side i.e sender's side. I am not the receiver of the information. All I know is the receiver expects me to send a data xml with UTF-8 encoding.

    When I send the first name as Melviñ, the value being passed for ñ is %F1, whereas the receiver (who is using UTF-8 encoding) expects it to be %C3%B1 for ñ.

    At sender's end, I am using an ecape function on the xml being passed along with UTF-8 encoding.
    URLDecoder.decode(path,"UTF-8")

    SO the question is : what should I do in order to send the value for ñ as %C3%B1 and not %F1 ?
     
    Paul Clapham
    Sheriff
    Posts: 21322
    32
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My Javascript book (the one with a rhinoceros on the cover) says about the escape() function:

    "Unicode characters \u0000 to \u00ff are replaced with the %xx escape sequence, and all other Unicode characters are replaced with the %uxxxx sequence."

    That's what you say you don't want, and based on your description I'd say you are correct in not wanting that.

    It says this about the encodeURI() function:

    "Any other characters in URI are replaced by converting each character to its UTF-8 encoding and then encoding each of the resulting one, two, or three bytes with a hexadecimal escape sequence of the form %xx."

    That sounds a lot more like what you want. So why didn't you take my advice and try switching from escape() to encodeURI()?
     
    Tiya Khambadkone
    Ranch Hand
    Posts: 110
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To make it simple,

    <script type="text/javascript">
    var uri="http://www.someXYZwebsite.com/my test.jsp?name=O'lviñ";
    document.write(escape(uri));
    </script>

    gives Output as :

    http%3A//www.someXYZwebsite.com/my%20test.jsp%3Fname%3DO%27lvi%F1

    the special character - apostrophe(') is escaped but the internationnal character (ñ) is not in UTF-8.

    I expect the output to be :

    http%3A//www.someXYZwebsite.com/my%20test.jsp%3Fname%3DO%27lvi%C3%B1

    If I use encodeURI , I get

    http%3A//www.someXYZwebsite.com/my%20test.jsp%3Fname=O'lvi%C3%B1

    what about the special character ?
     
    Paul Clapham
    Sheriff
    Posts: 21322
    32
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I dunno. Did it cause a problem? You didn't say you tried it.
     
    Tiya Khambadkone
    Ranch Hand
    Posts: 110
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    encodeURI works well to solve my problem but encodeURI does not work well if there is an apostrophe in my XML (eg. <lastName>O'Hare</lastName>).
    please suggest something that can work well with both - special characters as well as international characters.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65118
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Define "not work well".
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic