Titanium Community Questions & Answer Archive

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

createHTTPClient - Grab web content, parse, insert into another HTML page

Hey guys - I'm trying to grab the entire document from an external URL. That's a pretty simple thing to do, but I'm at a loss as to how I look through that data, grabbing everything within a specific element.

I would then like to insert that into an element in a loaded html document, within a Web View.

It was pretty simple before 1.0 (and Jquery), and I'm sure it is now, but I'm not sure where to make the adjustments.

Cheers!

Old way:

var content = Titanium.Network.createHTTPClient();
content.onload = function() {
  $("body").append("<div id='store'></div>");
  $("#store").html(this.responseText);
};

content.open('GET', 'http://www.website.com');
content.send();

New Way:

xhr.open("GET","http://www.website.com");
xhr.onload = function(){
  var doc = this.responseText;
  // Now what? :-)   
};

xhr.send();
— asked March 13th 2010 by Fred Spencer
  • createhttpclient
  • parse
  • xhr
0 Comments

1 Answer

  • Accepted Answer

    Try replacing your //now What? with the following:

        var statusCode = xhr.status;
        //check the response code returned
        if(statusCode == 200)
        {
            var doc = this.responseText;
            Titanium.API.log("info","Response: "+doc);
            var result = doc.documentElement.getElementsById("store").text();
    
            Ti.API.info('Store: '+result);
    
        }
    

    Hopefully that should work. I think your solution should lie in documentElement.getElementsById("store") or similar. This is how I parse XML:

        if(statusCode == 200)
        {
            Ti.API.info('>>> got the contacts xml! ... ');
            var xml = GETRequest.responseXML;
            Titanium.API.log("info","Response: "+xml);
            var result = xml.documentElement.getElementsByTagName("result");
            var itemList = xml.documentElement.getElementsByTagName("user");
            Ti.API.info('got item list');
            var cdata = [];
    
            //var id_text = $(this).attr('id');
            var fbid_text = itemList.item(0).getElementsByTagName("fb_ID").item(0).text;
            var gid_text = itemList.item(0).getElementsByTagName("g_ID").item(0).text;
    
        }
    

    Good luck!

    — answered March 13th 2010 by Ryan Gartin
    permalink
    0 Comments
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.