aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Javascript escape() function to handle an XML containing international characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Javascript escape() function to handle an XML containing international characters" Watch "Javascript escape() function to handle an XML containing international characters" New topic
Author

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

Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
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
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

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

Joined: Sep 15, 2011
Posts: 52
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

Joined: Jan 10, 2002
Posts: 61418
    
  67

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.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Tiya Khambadkone
    Ranch Hand

    Joined: Sep 15, 2011
    Posts: 52
    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    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

    Joined: Sep 15, 2011
    Posts: 52
    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18655
        
        8

    I dunno. Did it cause a problem? You didn't say you tried it.
    Tiya Khambadkone
    Ranch Hand

    Joined: Sep 15, 2011
    Posts: 52
    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

    Joined: Jan 10, 2002
    Posts: 61418
        
      67

    Define "not work well".
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Javascript escape() function to handle an XML containing international characters