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 node lists – Example of issues

Hi,

Firstly, sorry for the longer than usual post.

The following code snippets demonstrate where there appear to be issues in the current XML support in Ti Mobile.

This example will work. In this instance the element list is created directly from the document element.

//This should list all 13 nodes
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function()
{
    var data = [];
    var doc = this.responseXML.documentElement;
    var elements = doc.getElementsByTagName("node");
    for (var i=0;i<elements.length;i++) {
        var row = Ti.UI.createTableViewRow();
        row.title = elements.item(i).getAttribute("id");
        data[i]=row;
    } 
    var tableview = Titanium.UI.createTableView({
       data:data,
    });
    Titanium.UI.currentWindow.add(tableview);   
};
xhr.open('GET','http://localhost/~mark/xmltext.xml');
xhr.send();

This next example should display all node elements but will only display 3. The elements list this time is created from the variable nodes i.e. not directly from document element.

//This should list all 13 nodes
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function()
{
    var data = [];  
    var doc = this.responseXML.documentElement;
    var nodes = doc.getElementsByTagName("nodes");
    var elements = nodes.item(0).getElementsByTagName("node");
    for (var i=0;i<elements.length;i++) {
        var row = Ti.UI.createTableViewRow();
        row.title = elements.item(i).getAttribute("id");
        data[i]=row;
    } 
    var tableview = Titanium.UI.createTableView({
       data:data,
    });
    Titanium.UI.currentWindow.add(tableview);   
};
xhr.open('GET','http://localhost/~mark/xmltext.xml');
xhr.send();

Here's the XML file I have been using.

<?xml version="1.0" encoding="UTF-8"?>
<response>
        <nodes id="nodes">
            <node id="node 1">
                <node id="node 2">
                    <node id="node 3"/>
                </node>
                <node id="node 4">
                    <node id="node 5"/>
                </node>
                <node id="node 6">
                    <node id="node 7"/>
                </node>
            </node>
            <node id="node 8">
                <node id="node 9">
                    <node id="node 10"/>
                </node>
            </node>
            <node id="node 11">
                <node id="node 12">
                    <node id="node 13"/>
                </node>
            </node>
        </nodes>
</response>

Cheers
Mark

— asked March 19th 2010 by Mark Poston
  • 1.0
  • documentelement
  • dom
  • example
  • issue
  • nodelist
  • xml
0 Comments

4 Answers

  • As per above your problem u want to get value of elements in table view row so i solve it out in this way..

    var doc = this.responseXML.documentElement;
    var elements = doc.getElementsByTagName("title");
    var extras = doc.getElementsByTagName("feature");
    for(i=0; i <elements.length; i++)
    {
    data[i]= {title:"" + elements.item(i).text};
    }
    After this you have to add the value in tableview as below

    var tableViewOptions = {
    data: data,
    top:40,
    style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
    //headerTitle:'TableView examples',
    //footerTitle:"Wow. That was cool!",
    backgroundColor:'transparent',
    shadowColor:'#aaa',
    shadowOffset:{x:5,y:5},
    rowBackgroundColor:'white'
    };

    var row = Titanium.UI.createTableViewRow();

    var tableview = Titanium.UI.createTableView(tableViewOptions);
    tableview.addEventListener('click', function(e)
    {
        if (e.rowData.test)
            {
                var win = Titanium.UI.createWindow({
                url:e.rowData.test,
                title:e.rowData.title
            });
        Titanium.UI.currentTab.open(win,{animated:true});
            }
    });
    Ti.UI.currentWindow.add(tableview);
    

    };

    Try it .. if u have again problem then do comment.

    — answered July 20th 2011 by shweta dodiya
    permalink
    0 Comments
  • good find. will open an issue for this.

    — answered March 19th 2010 by Jeff Haynie
    permalink
    0 Comments
  • Hi Jeff,
    Thanks for opening it as an issue.
    Following on from my above examples, the issues I have found so far seem to all stem from the fact that creating any node list or querying of nodes is fine when they are taken directly from the documentElement, but fail when they are not.
    I reported this issue a week or so ago: http://developer.appcelerator.com/question/91/getelementsbytagname-issue.
    And this a few days back: http://developer.appcelerator.com/question/3451/xml-dom-childnodes-issue
    It seems to me that there is perhaps a single issue with the XML support that then manifests itself in all of the examples I can provide. That is that the common theme is they are all using or creating node lists that are not created directly from documentElement.
    I had started to create a number of examples based on the same code examples above that try to populate a tableview, here's a few more I have found that you should be able to also confirm. Using the nextSibling property also seems to fail (see my other post for a nodewalker code example).
    Please also check the log for the [TiDOMElementProxy kind]: unrecognized selector sent to instance exception that some of my examples will throw.

        var doc = this.responseXML.documentElement;
        var nodes = doc.getElementsByTagName("nodes");
        var elements = nodes.item(0).childNodes;
    

    and

        var doc = this.responseXML.documentElement;
        var elements = doc.firstChild.childNodes; //expect response.nodes.node(*3) in my xml file
    

    and

        var data = [];
        var doc = this.responseXML.documentElement;
    
        var row = Ti.UI.createTableViewRow();
        row.title = doc.firstChild.getAttribute("id") + " should be 'nodes'";
        data[0]=row;
    
        var row = Ti.UI.createTableViewRow();
        row.title = doc.firstChild.firstChild.getAttribute("id") + " should be 'node 1'";
        data[1]=row;
    
        var row = Ti.UI.createTableViewRow();
        row.title = doc.firstChild.firstChild.firstChild.getAttribute("id") + " should be 'node 2'";
        data[2]=row;
    

    I hope these further examples help to narrow down the root of the problem ;)
    Regards
    Mark

    — answered March 20th 2010 by Mark Poston
    permalink
    0 Comments
  • these issues have been resolved and will be in tomorrow's 1.1 release.

    — answered March 22nd 2010 by Jeff Haynie
    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.