Titanium Community Questions & Answer Archive

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

How can I parse HTML tag into Window on android?

I have an HTML tag as below:

<b>Title</b>
<i>Description</i>

I would like to parse this to window without using webview. How could I do so? please give me some suggestion.

var win = Ti.UI.createWindow({
  fullscreen: false
});

win.open();

The result would be:

Title
Description

— asked November 3rd 2010 by Den Cambodia
  • android
  • html
0 Comments

3 Answers

  • As scraping web pages can be resource-intensive, if it were me, I would write a web service running on a web server that would retrieve the data for the mobile app, returning the data in json format.

    Other people may have alternative views on this however.

    — answered November 5th 2010 by Paul Dowsett
    permalink
    0 Comments
  • I am working with a client that would rather not introduce a server layer at this juncture. Has anybody found a solution for this?

    — answered March 29th 2011 by nick c
    permalink
    0 Comments
  • I don't know where you are with this, but I was able to put together a function that pulls content from a particular div on a web site by class:

    //Pull HTML content from div tag
    function getDivContent(url,className,element) {
        var client = Ti.Network.createHTTPClient();
        //open url
        client.open('GET', url);
        client.onload = function(){
              //get content of url in string
            var doc = this.responseText;
            //create RegEx expresison to search for div tags
            var pattern = new RegExp('<div class=\"' + className + '\">[\\s\\S]*?<\/div>');
            var content = "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>" + pattern.exec(doc) + "</body>";
           Ti.API.info('Store: '+ content);
           //replace webview content
           element.html = content;
        }
        client.send();    
    }
    

    I then can reference it like so in my ui.js:

    var aboutLabelDescription = Titanium.UI.createWebView({
            width:'90%',
            html:'',
            top:20,
            height:'95%',
            borderWidth:10,
            borderColor:'#fff',
            borderRadius:15
    });
    
    getDivContent('http://pianocreations.com/about/','entry',aboutLabelDescription);
    

    Just a note that this script may break if you have nested divs within your divs on the html page that you are referencing.

    — answered January 23rd 2012 by Stefan Holodnick
    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.