Return XML tag attribute when tag value matches input using JavaScript

angeloumber

New Member
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"\[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>\[/code\]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:\[code\]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=http://stackoverflow.com/questions/10919518/=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;}\[/code\]The output in this case should be:\[code\] var output = "US-MI-081";\[/code\]I greatly appreciate your help. +1 and more to anybody who can provide me with a working function! Thanks in advance!!
 
Back
Top