Pergunta

I am working with the XML code shown below. I have already requested and retrieved the code. I need to write a function that will: 1) receive the inputted code 2) dynamically return the subnational2-code in the database that corresponds with the subnational1-code="US-MI" and the tag value "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.

The following is code I have either used in the past (getData worked), or am trying to figure out. This does not necessarily have to be used:

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;
}

The output in this case should be:

 var output = "US-MI-081";

I greatly appreciate your help. +1 and more to anybody who can provide me with a working function! Thanks in advance!!

Foi útil?

Solução

Not sure if this is entirely what you where looking for, but here is a javascript way to parse it out. Hopefully this will work for you.

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

Outras dicas

I would use XPath to retrieve the node.

Try using the document.evaluate() function:

var xpathResult = document.evaluate("location[@subnational1-code='US-MI' and text() = 'Kent']/@subnational2-code", itemList, null, XPathResult.ANY_TYPE, null);  
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top