XML Parsing failure on Android but works on iPhone

I am preparing a general application which works both for iPhone and Android phones with same code for which Titanium is majorly used for. With the same code I found different results for XML document parsing and my android app does not load it properly. It causes NULLPointer Exception. But the same code works perfectly on iPhone.

Can you check this?

xyz(languageCode, currentVersion, xmldoc) {
try {
  Ti.API.info('Start parsing the library');
  var archiveData = xmldoc.getElementsByTagName('archive');
  Ti.API.info('Archive: ' + archiveData);
  Ti.API.info('Item: ' + archiveData.item);
  Ti.API.info('Lendth: ' + archiveData.length);
  var newVersion = archiveData.item(0).getAttribute('version');
  Ti.API.info('Library Went In Catch: ' + e);

Function call: It's from onload() method of Ti.Network.HttpClient

xyz(languageCode, version,this.responseXML.documentElement);

Output of iPhone:

Archive: [object TiDOMNodeList]
[INFO] Item: [object TiDOMNodeList]
[INFO] Lendth: 1

Output of Android:

[INFO] [3,7225] Archive: [Ti.NodeList]
[INFO] [1,7226] Item: [KrollMethod item]
[INFO] [2,7228] Lendth: 0
[INFO] [235,7466] Library Went In Catch: JavaException: java.lang.NullPointerException: null

2 Answers

  • Accepted Answer

    Ah yes, this cached my eye: "this.responseXML.documentElement". It has some problems on Android (don't know is it reported or not). The workaround it to use "Ti.XML.parseString" method to re-parse XML:

    var xml = Ti.XML.parseString(this.responseXML.toString());
    xyz(languageCode, version, xml);

    This is untested, but if I recall from my memory I think it will work fine. Hope it helps! :)

  • can you post the xml ?

    — answered 4 years ago by Ophir 'Shlomi' Oren
    • The xml demo as you requested:

      <archive version="9.7">
          <timeoffset code="KGF.L" offset="-1"/>
          <timeoffset code="KGFHY.PK" offset="-6"/>
              <media id="104" description="Kingfisher is Europe’s leading home improvement retailer and the third largest in the world, with nearly 900 stores in 8 countries in Europe and Asia. We have market-leading positions in the UK, France, Poland, Turkey and China." version="1.2" img="" imgVersion="1.2"/>
          <section name="Annual Reports" id="1" displayno="5" type="1">
              <media id="1" name="Annual report 2010/11" version="1.1" displayno="3" type="1"/>
              <media id="2" name="Annual report 2009/10" version="1.1" displayno="2" type="1"/>
              <media id="3" name="Annual report 2008/09" version="1.1" displayno="1" type="1"/>
          <section name="Trading Updates" id="2" displayno="4" type="1">
               <media id="106" name="Interim Results 2011/12 (Part 1)(French)" version="2.0" displayno="16" type="1"/>
              <media id="101" name="Interim Results 2011/12 (Part 1)" version="0.2" displayno="15" type="1"/>
              <media id="100" name="Interim Results 2011/12 (Part 2)" version="0.2" displayno="14" type="1"/>

      — commented 4 years ago by Paresh Thakor
    • But it works perfect with iPhone. It has issues only with Android. I have executed same XML and same code for iPhone but it is working fine. don't know somehow fails for Android and crashes giving NULL pointer exception.

      — commented 4 years ago by Paresh Thakor