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

时间:2015-09-04 23:35:32

标签: javascript xml titanium tableview titanium-mobile

所以我试图从XML文件中获取数据,但是当我通过它循环并希望得到" countyname"我只获得第一个,然后停止。 XMl错了吗?因为我尝试使用其他XML,并且我的代码能够将数据导入到我的tableview中。 最好的祝福! 菲利普

<?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>

app.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();

county.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();

city.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 个答案:

答案 0 :(得分:1)

您的代码逻辑错误。让我解释一下为什么......我将对您的代码进行评论,最后您将了解它的错误。

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

通过这一行代码,您可以获得所有&#34; county&#34; XML文件的元素。在您的情况下,只有一个元素。因此itemsNode.List,只包含一个元素。

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

使用for语句,您将遍历items中的所有元素。换句话说,for语句的内容将重复items.lenght次。但是items只包含一个元素!所以不会有迭代。

在您的陈述中,您要创建新行。但是只会创建一行,因为没有迭代。出于这个原因,你只得到第一个&#34; countyname&#34;标签

我希望你能理解你的错误......现在我为你的问题提供一个简单的解决方案

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;

我的代码只列出标签名称为&#34; countyname&#34;的所有元素。根据您的XML文件,items将是Node.List,其中包含两个元素。然后使用for语句,可以从列表中每个节点的textContent创建新行!

答案 1 :(得分:-1)

检查xml,

开始

<food_company>
   <county>

结束

   </lan>
</food_company>