代码之家  ›  专栏  ›  技术社区  ›  Filip Lang

XML数据只显示其中的一部分

  •  -1
  • Filip Lang  · 技术社区  · 9 年前

    所以我试图从一个XML文件中获取数据,但当我循环通过它并想要得到“countyname”时,我只得到了第一个,然后它就停止了。XMl错了吗?因为我尝试过使用其他XML,这与将数据输入到表视图的代码配合得很好。 顺致敬意, 菲利普

    <?xml version="1.0" encoding="UTF-8"?>
    <food_company>
    <county>
        <countyname>New York</countyname>
        <city>
        <cityname>New York City</cityname>
            <restaurant>
                <name>Dinos pizzeria</name>
                <phone>01111111</phone>
                <location>broadway1</location>
            </restaurant>
            <restaurant>
                <name>Dinos pizzeria2</name>
                <phone>01111111</phone>
                <location>broadway2</location>
            </restaurant>
            <restaurant>
                <name>Dinos pizzeria3</name>
                <phone>01111111</phone>
                <location>broadway3</location>
            </restaurant>
        </city>
          <countyname>Baldwin County</countyname>
        <city>
        <cityname>Bay Minette</cityname>
            <restaurant>
                <name>Dinos pizzeria</name>
                <phone>01111111</phone>
                <location>broadway1</location>
            </restaurant>
            <restaurant>
                <name>Dinos pizzeria2</name>
                <phone>01111111</phone>
                <location>broadway2</location>
            </restaurant>
            <restaurant>
                <name>Dinos pizzeria3</name>
                <phone>01111111</phone>
                <location>broadway3</location>
            </restaurant>
        </city>
    </lan>
    </food_company>
    

    应用程序。js代码:

    Titanium.UI.setBackgroundColor('#E1E6EE');
    
    
    
    // create base UI tab and root window
    var win1 = Titanium.UI.createWindow({  
    
    
    statusBarStyle: Ti.UI.iPhone.StatusBar.LIGHT_CONTENT,
    tintColor: '#FFF',
    backgroundColor:'#E1E6EE',
    url: 'lan.js',
    tabBarHidden: true,
    navBarHidden: true
    });
    
    win1.open();
    

    县js代码:

    Ti.include('app_functions.js');
    
    var win = Titanium.UI.currentWindow;
    
    // create a table to display news feeds--------------------------------
    var itemsTable = Ti.UI.createTableView({
    top : '11%',
    left : 0,
    leftImage : 'taxi.png',
    backgroundColor : '#DCEEDC', //E1E6EE
    bottom : '0%',
    // search : searchBar,
    filterAttribute : 'searchFilter'
    });
    win.add(itemsTable);
    
    // define xmlFeed (you can customize this with any RSS feed)
    var xmlFeed = 'http://eventverket.nu/test/test5.xml';
    //'http://83.254.164.137:1000/test.xml';
    
    // create a new HTTP client object
    var xhr = Ti.Network.createHTTPClient();
    
    // this method will process the remote data
    xhr.onload = function() {
    
    // create an xml object
    var xml = this.responseXML;
    
    // create an array that will store news items for our tableView
    var data = [];
    var data = [];
    var items = xml.documentElement.getElementsByTagName("county");
    for (var i=0; i<items.length; i++) {
    var row = Ti.UI.createTableViewRow({
        title: items.item(i).getTextContent()
    }); 
    data.push(row); 
    }
    itemsTable.data = data;
    
    // when the user clicks on a row
    itemsTable.addEventListener('click', function(e) {
    
    // NEW WINDOW
    var newWindow = Titanium.UI.createWindow({
        backgroundColor : '#DCEEDC', //E1E6EE
        statusBarStyle : Ti.UI.iPhone.StatusBar.LIGHT_CONTENT,
        font : fonts[16]['normal'],
        url : "stad.js",
        //backButtonTitle: 'Back',
        //title: e.source.title,
        tabBarHidden : true,
        navBarHidden : true,
        tintColor : '#FFF'
        });
    
        newWindow.open();
       });
    
    };
    
    // this method will be called if there is an error in accessing the     data
    xhr.onerror = function() {
        // hide activity indicator
    activityIndicator.hide();
    
    // display error
    alert(this.status + ': ' + this.statusText);
    return false;
    };
    
    // open the remote feed
    xhr.open('GET', xmlFeed);
    
    // execute the call to the remote feed
    xhr.send();
    

    城市js代码:

    Ti.include('app_functions.js');
    
    var newWin = Titanium.UI.currentWindow;
    
    
    
    // create a table to display news feeds--------------------------------
    var itemsTable = Ti.UI.createTableView({
    top : '11%',
    left : 0,
    leftImage : 'taxi.png',
    backgroundColor : '#DCEEDC', //E1E6EE
    bottom : '0%',
    // search : searchBar,
    filterAttribute : 'searchFilter'
    });
    win.add(itemsTable);
    
    // define xmlFeed (you can customize this with any RSS feed)
    var xmlFeed = 'http://eventverket.nu/test/test5.xml';
    //'http://83.254.164.137:1000/test.xml';
    
    // create a new HTTP client object
    var xhr = Ti.Network.createHTTPClient();
    
    // this method will process the remote data
    xhr.onload = function() {
    
    // create an xml object
    var xml = this.responseXML;
    
    // create an array that will store news items for our tableView
    
    var data = [];
    
    
    
    var items = xml.documentElement.getElementsByTagName("city");
    for (var i=0; i<items.length; i++) {
    var row = Ti.UI.createTableViewRow({
        title: items.item(i).getTextContent() //
    }); 
    data.push(row); 
    }
    itemsTable.data = data;
    
    
    
    
    
    // when the user clicks on a row
    itemsTable.addEventListener('click', function(e) {
    
    // NEW WINDOW
    var newWindow = Titanium.UI.createWindow({
        backgroundColor : '#DCEEDC', //E1E6EE
        statusBarStyle : Ti.UI.iPhone.StatusBar.LIGHT_CONTENT,
        font : fonts[16]['normal'],
        url : "stad.js",
        //backButtonTitle: 'Back',
        //title: e.source.title,
        tabBarHidden : true,
        navBarHidden : true,
        tintColor : '#FFF'
        });
    
    
     });
    
    };
    
    // this method will be called if there is an error in accessing the data
    xhr.onerror = function() {
    // hide activity indicator
    activityIndicator.hide();
    
    // display error
    alert(this.status + ': ' + this.statusText);
    return false;
    };
    
    // open the remote feed
    xhr.open('GET', xmlFeed);
    
    // execute the call to the remote feed
    xhr.send();
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   Riccardo Bucco    9 年前

    你的代码逻辑是错误的。让我解释一下为什么……我将对你的代码进行注释,最后你会明白它有什么问题。

    var items = xml.documentElement.getElementsByTagName("county");
    

    使用这行代码,您将获得XML文件的所有“country”元素。在您的案例中,只有一个元素。所以 items 是一个 Node.List 其仅包含一个元素。

    for (var i=0; i<items.length; i++) {
        ... 
    }
    

    使用 for 语句中的所有元素 项目 。换句话说 对于 语句将重复 items.lenght 时间。但是 项目 仅包含一个元素!所以不会有迭代。

    在语句中,您正在创建新行。但由于没有迭代,所以只会创建一行。因此,您只能获得第一个“countyname”标签。

    我希望你明白你的错误…现在我给你一个 简单解决方案 针对您的问题:

    var data = [];
    var items = xml.documentElement.getElementsByTagName("countyname");
    for (var i=0; i<items.length; i++) {
        var row = Ti.UI.createTableViewRow({
            title: items.item(i).getTextContent()
        }); 
        data.push(row); 
    }
    itemsTable.data = data;
    

    我的代码只是得到了标记名为“countyname”的所有元素的列表。根据您的XML文件 项目 将是 节点.列表 具有两个元件。然后用 对于 语句,可以从列表的每个节点的textContent创建新行!

        2
  •  -1
  •   Roy    9 年前

    检查xml,

    开始于

    <food_company>
       <county>
    

    以结尾

       </lan>
    </food_company>