Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

XML parser unable to correctly handle numeric entities (Android)

Hi,

I've been struggling for a while around the following issue with the android XML parser (Ti Mobile SDK 1.4).
In particular, if an XML file (or HTTP xml response message) contains some numeric entities for special characters (e.g. ë), these are correctly handled only when they are found in an attribute, but not when they are present inside the inner text of an element.

As an example, take the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<meteo>
  <fcast day="Venerd&#236; 27 agosto 2010">
    Prevalentemente nuvoloso con rovesci anche temporaleschi
    pi&#249; insistenti lungo la dorsale di confine.
  </fcast>
</meteo>

I have the "day" attribute of the fcast element correctly parsed, as "Venerdì 27 agosto", while the numeric entities present in the inner text of the node are simply elided in the parsed text, e.g. with:

[...]
var meteo = xmlDoc.getElementsByTagName('meteo').item(0);
var details = meteo.item(0).getElementsByTagName('fcast').item(0).text;

Ti.API.info(details);

the result is:

"Prevalentemente nuvoloso con rovesci anche temporaleschi pi insistenti lungo la dorsale di confine."
Where the bold text is supposed to be "più".

Please note that the same code works like a charm on iOS.

At the moment I've been able to resolve the issue by simply pre-parsing the XML string (either loaded from file, or received in an http response), by substituting the numeric entities with their corresponding unicode encodings.

Have a nice day.

 Olivier
— asked August 27th 2010 by Olivier Morandi
  • android
  • mobile
  • parser
  • titanium
  • xml
0 Comments

5 Answers

  • var xml_special_to_escaped_one_map = {
    '&': '&amp;',
    '"': '"',
    '<': '<',
    '>': '>'
    };
    
    var escaped_one_to_xml_special_map = {
    '&amp;': '&',
    '"': '"',
    '<': '<',
    '>': '>'
    };
    
    function encodeXml(string) {
    return string.replace(/([\&"<>])/g, function(str, item) {
    return xml_special_to_escaped_one_map[item];
    });
    };
    
    function decodeXml(string) {
    return string.replace(/("|<|>|&amp;)/g,
    function(str, item) {
    return escaped_one_to_xml_special_map[item];
    });
    }
    

    You can add your own caracters…

    — answered September 8th 2010 by Ivan Mathy
    permalink
    0 Comments
  • var xml_special_to_escaped_one_map = {
    '&': '&amp;',
    '"': '"',
    '<': '<',
    '>': '>'
    };
    
    var escaped_one_to_xml_special_map = {
    '&amp;': '&',
    '"': '"',
    '<': '<',
    '>': '>'
    };
    
    function encodeXml(string) {
    return string.replace(/([\&"<>])/g, function(str, item) {
    return xml_special_to_escaped_one_map[item];
    });
    };
    
    function decodeXml(string) {
    return string.replace(/("|<|>|&amp;)/g,
    function(str, item) {
    return escaped_one_to_xml_special_map[item];
    });
    }
    

    You can add your own caracters…

    — answered September 8th 2010 by Ivan Mathy
    permalink
    0 Comments
  • Hello Olivier

    Sorry for this message, but this is the only way to contact you…

    Many of members of this site have read your message about geo-augmented reality…

    Is it possible that you publish this code, or send it to me : Ivan . Mathy [at] free . Fr

    It would help many of us…

    Thanks, and sorry for my bad english'

    Ivan

    — answered September 10th 2010 by Ivan Mathy
    permalink
    1 Comment
    • Hi Ivan, please take a look here. I'll post my code during this week.

      — commented September 13th 2010 by Olivier Morandi
  • @Olivier would you enter a Lighthouse Ticket with example XML that should be parsed and assign it to me. I don't see this particular issue in the system.

    — answered September 15th 2010 by Don Thorp
    permalink
    1 Comment
    • This bug is already fixed in git: https://appcelerator.lighthouseapp.com/projects/32238/tickets/862-on-android-cdata-from-xml-returns-blank

      — commented September 16th 2010 by Brion Vibber
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.