我正在使用如下所示的 XML 代码。我已经请求并检索了代码。我需要编写一个函数,它将:1)接收输入的代码 2)动态返回数据库中与 subnational1-code="US-MI" 和标签值“Kent”相对应的 subnational2-code
<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 和更多!提前致谢!!