2015-09-04 51 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