我正在使用下面顯示的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>
以下是代碼我在過去要麼使用(工作的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和更多給任何可以爲我提供工作功能的人!提前致謝!!
是jQuery的一個選項嗎? http://api.jquery.com/jQuery.parseXML/ –
不可以。這是在Google小工具內,不會接受或下載jQuery庫,不幸的是 – Wagtail