当标记值与使用JavaScript的输入匹配时,返回XML标记属性

时间:2012-06-06 17:55:41

标签: javascript xml xml-parsing google-gadget xmldom

我正在使用下面显示的XML代码。我已经请求并检索了代码。我需要编写一个函数:   1)接收输入的代码   2)动态返回与subnational1-code =“US-MI”对应的数据库中的subnational2-code和标记值“Kent”

<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以上!在此先感谢!!

2 个答案:

答案 0 :(得分:1)

我会使用XPath来检索节点。

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

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

答案 1 :(得分: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
相关问题