Titanium Community Questions & Answer Archive

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

Catch a parse error

Anyone knows how to catch a parse error from this.responseXML ?

Sometimes when fetching a xml input, it times out. Timeout message can appear inside the xml data and the parsing will bail out. I want to make attempts to fetch new data if the xml parsing fails.

— asked April 3rd 2010 by Thor Svensson
  • 1.1.2
  • parse
  • xml
0 Comments

8 Answers

  • if you want to catch the parse error then you can just get the response text and parse it in your own code.

    wrap your Ti.XML.parse(xhr.responseText) in a try catch block

    — answered March 27th 2011 by Aaron Saunders
    permalink
    1 Comment
    • Ti.XML.parseString(xhr.responseText); and you're golden!

      — commented April 28th 2011 by Mark Henderson
  • did you find a solution for this error ? i have the same sometimes. thank you

    — answered March 27th 2011 by Djamel ZAHAL
    permalink
    0 Comments
  • Using a try-catch statement works well enough for me. Looks something like this:

    xhr.onload = function()
    {
        try
        {    
            var doc = this.responseXML.documentElement;
            saveXML(doc);
    
            populateTableView(doc); // normal flow
    
            hideLoadingView();
        }
        catch(e)
        {
            showLoadingViewRefresh(0); // code to be executed on error
        }
    };
    
    — answered April 26th 2012 by George Marmaridis
    permalink
    0 Comments
  • Are you setting an error listener on HTTP client? Which platform are you testing on?

    — answered April 4th 2010 by Don Thorp
    permalink
    0 Comments
  • iPhone OS

    Yes, but it does not catch this parser error:

    Entity: line 43: parser error : Premature end of data in tag picture line 42
    ^

    [WARN] Error Domain=com.google.GDataXML Code=-1 "Operation could not be completed. (com.google.GDataXML error -1.)". in -[TiDOMDocumentProxy parseString:] (TiDOMDocumentProxy.m:45)

    This is the code:

    var xhr = Titanium.Network.createHTTPClient();

    xhr.onerror = function(e)
    {
    Ti.API.info("What ERROR -> " + e.error);
    };

    xhr.onload = function()
    {
    var statusCode = this.status;
    Titanium.API.log("info", " -> Status code: " + statusCode);

    var XMLtext = this.responseXML;
    

    etc. etc.

    — answered April 4th 2010 by Thor Svensson
    permalink
    0 Comments
  • Psst. Did you ever figure out what to do about this? I've got a really similar error, and I'm not sure what to do about it.

    — answered July 10th 2010 by Micah Rich
    permalink
    0 Comments
  • Timeouts should be catch on xhr's onerror. Try setting a timeout and see if that gets triggered.

    — answered July 10th 2010 by Sj Singh
    permalink
    0 Comments
  • No I have not found any solution to this. xhr's onerror does not catch parse error. When it times out I can't figure out the reason, only it timed out :-(

    — answered July 10th 2010 by Thor Svensson
    permalink
    1 Comment
    • A couple of approaches spring to mind to help debug where the problem is - first turn off async when you open with xhr as it'll help trap immediate issues. Next - open the url with your normal browser (Chrome or FF) and check the return headers to see if anything is looking odd.

      Finally - use the responseText rather than responseXML and push the content through Titanium.XML.parseString to get a dom document. It might throw up a catchable error or help diag the issue further.

      — commented July 10th 2010 by David Ashwood
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.