1

我正在使用如下所示的 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>

XML 代码 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 和更多!提前致谢!!

4

2 回答 2

1

我会使用 XPath 来检索节点。

尝试使用 document.evaluate() 函数:

var xpathResult = document.evaluate("location[@subnational1-code='US-MI' and text() = 'Kent']/@subnational2-code", itemList, null, XPathResult.ANY_TYPE, null);  
于 2012-06-08T21:48:19.097 回答
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
于 2012-06-08T22:15:56.150 回答