2015-09-04 50 views
-1

所以我试图从一个XML文件中获取数据,但是当我通过它循环并想要出去“县名”时,我只得到第一个,然后停止。 XMl是否错误?因为我尝试过使用其他XML,并且能够将我的代码获取到我的tableview中的数据。 此致敬礼! 菲利普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> 

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(); 
+1

_你认为这个问题在不知道你的代码的情况下可以回答吗?_ ** NO **。 寻求调试帮助的问题(“为什么这个代码不工作?”)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。 – baao

+0

嗨迈克尔! 我很抱歉,我没有想到它通过! 这是我的代码:http:// pastie。组织/ 10397882 这里是我得到的XML数据: \t \t'VAR项目= xml.documentElement.getElementsByTagName( “县”);' \t'//通过每个回路item' \t'为( VAR I = 0; I

+0

,请改正它,如果这是不对的。无论何时,只要您提问,请随时在您的问题中包括您的代码。这将帮助你获得答案 – baao

回答

1

你的代码的逻辑是错误的。让我解释为什么......我要评论你的代码,最后你会明白它有什么问题。

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

通过这行代码,您将获得XML文件的所有“县”元素。在你的情况下,只有一个元素。所以items是一个Node.List,它只包含一个元素。

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

随着你通过所有的元素items迭代for声明。换句话说,for声明的内容将重复items.lenght次。但是items只包含一个元素!所以不会有迭代。

在您的声明中,您正在创建新行。但是只有一行将被创建,因为没有迭代。出于这个原因,你只能得到第一个“郡名”标签。

我希望你理解你的错误......现在我给你一个简单的解决方案您的问题:

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文件items将是一个Node.List与两个元素。然后使用for语句,可以从列表中每个节点的textContent创建新行!

+0

非常感谢你@ Riccardo Bucco! 这工作格里特! 但我还有一个问题!当我按下“纽约”时,我只想让'纽约市'出现,但现在两个城市名都出现了,是否有办法做到这一点?最好的问候菲利普 –

+0

不客气!但请向我们展示您的代码:如果没有它,无法帮助您!我不知道你是如何实现它的。由于当前问题已解决,请将我的答案标记为“已接受”。然后打开另一个问题,详细介绍另一个问题:请记住给我们尽可能多的细节,以便能够帮助你(并告诉我们你已经做了什么!!) –

+0

嗨@Riccardo Bucco! 现在我添加了我所有的代码! 我想要做的是:得到所有的countys,然后按县,并获得该县的所有城市。我不能按“向上箭头”,因为我没有15分:/ 此致敬意。 Filip –

-1

检查XML,

入门

<food_company> 
    <county> 

结束了

</lan> 
</food_company> 
+0

嗨@roy! 对不起,这是我的错误!我把它从瑞典语翻译过来,只是忘了将“lan”(县)翻译为英语,因为它不是我的意见文章 –

+0

我不能评论他的帖子,我认为它可能会帮助他...所以在这里发布 – Roy