2012-06-06 20 views
1

我正在使用下面顯示的XML代碼。我已經請求並檢索了代碼。我需要編寫一個函數: 1)接收輸入的代碼 2)動態返回數據庫中對應國家1-代碼=「US-MI」和標記值「Kent」的國家二級代碼當標籤值與使用JavaScript的輸入匹配時,返回XML標籤屬性

<result> 
    <location country-code="US" subnational1-code="US-TX" subnational2-code="US-TX-263">Kent</location> 
    <location country-code="US" subnational1-code="US-VA" subnational2-code="US-VA-127">New Kent</location> 
    <location country-code="US" subnational1-code="US-DE" subnational2-code="US-DE-001">Kent</location> 
    <location country-code="US" subnational1-code="US-KY" subnational2-code="US-KY-117">Kenton</location> 
    <location country-code="US" subnational1-code="US-MD" subnational2-code="US-MD-029">Kent</location> 
    <location country-code="US" subnational1-code="US-MI" subnational2-code="US-MI-081">Kent</location> 
    <location country-code="US" subnational1-code="US-RI" subnational2-code="US-RI-003">Kent</location> 
    <location country-code="CA" subnational1-code="CA-NB" subnational2-code="CA-NB-KE">Kent</location> 
    <location country-code="CA" subnational1-code="CA-ON" subnational2-code="CA-ON-KT">Chatham-Kent</location> 
    <location country-code="GB" subnational1-code="GB-ENG" subnational2-code="GB-ENG-KEN">Kent</location> 
</result> 

XML Code URL.

以下是代碼我在過去要麼使用(工作的getData),或正在試圖找出。這並不一定必須使用:

var subnational1-code="US-MI"; 
var subnational2-name="Kent"; 
var itemList = response.getElementsByTagName("result"); 
for(i=0;i<itemList.length){ 
    var d = getData(itemList.item(i)); 

    // What code goes here?? 
    // If (subnational2-value==subnational2-name&&subnational1-valuue=="US-MI"); 

} 

function getData(n) { 
    var d = new Object(); 
    var nodeList = n.childNodes; 
    for (var j = 0; j < nodeList.length ; j++) { 
    var node = nodeList.item(j); 
    d[node.nodeName] = node.firstChild.nodeValue; 
    } return d; 
} 

在這種情況下,輸出應爲:

var output = "US-MI-081"; 

我非常感謝你的幫助。 +1和更多給任何可以爲我提供工作功能的人!提前致謝!!

+1

是jQuery的一個選項嗎? http://api.jquery.com/jQuery.parseXML/ –

+0

不可以。這是在Google小工具內,不會接受或下載jQuery庫,不幸的是 – Wagtail

回答

1

不知道這是什麼完全您要尋找的,但這裏是解析出來一個JavaScript的方式。希望這會對你有用。

var output = ""; 
var subnational1Code="US-MI"; 
var subnational2Name="Kent"; 
var itemList = document.getElementsByTagName("result"); 
for(var i = 0;i<itemList.length; i++){ 
    var d = getData(itemList.item(i)); 
    for(var k in d) 
    if (d[k].sub1 === subnational1Code && d[k].value === subnational2Name) 
     output = d[k].sub2; 
} 
function getData(n) { 
    var objArray = new Array(); 
    var nodeList = n.childNodes; 
    for (var j = 0; j < nodeList.length ; j++) { 
    var node = nodeList.item(j); 
    var o = new Object(); 
    o["value"] = node.firstChild.nodeValue; 
    o["country"] = node.attributes["country-code"].nodeValue; 
    o["sub1"] = node.attributes["subnational1-code"].nodeValue; 
    o["sub2"] = node.attributes["subnational2-code"].nodeValue; 
    objArray.push(o); 
    } return objArray; 
} 

// console.log(output); //in case you wanted to debug it 
+0

非常感謝您的回答!當我有機會時,我會檢查它:)我認爲它看起來不錯 – Wagtail

+1

不客氣。請讓我知道這對你有沒有用。 :) –

+0

嗯,我有麻煩。我瞭解代碼,但它不起作用。第7-9行是否有一些括號?你有沒有在網址上測試過它,它是否有效?你能複製一個jsFiddle什麼的?我希望我不要問太多: - | – Wagtail

1

我會使用XPath來檢索節點。

嘗試使用document.evaluate()函數:如果

var xpathResult = document.evaluate("location[@subnational1-code='US-MI' and text() = 'Kent']/@subnational2-code", itemList, null, XPathResult.ANY_TYPE, null); 
+0

我正在開發Google小工具,可能不會支持XPath。我正在進行研究以查找Google代碼等效物。在這種情況下,純JavaScript解析將是首選。 – Wagtail