0

I am trying to pull an xml node's value by referencing the value of another node.

Here is an snippet of one of my nodes

<document>
 <row>
  <DISTRICT>100</DISTRICT>
  <BIOS>BROWN</BIOS>
  <AREA_KM>3663.158164</AREA_KM>
  <AREA_MI>1414.347616</AREA_MI>
  <NAME>100</NAME>
  <REG>1</REG>
  <ACRES>905182</ACRES>
  <EMU_Name>Purcell</EMU_Name>
  <Shape_Leng>299746.4938</Shape_Leng>
  <Shape_Area>3663158164</Shape_Area>
  <LegalDesc>Northeast of District 151</LegalDesc>
 </row>
</document>

I'd like to fill in some HTML with value from the LegalDesc field based on the # in the DISTRICT field.

for example, I'd like to display the LegalDesc for DISTRICT==100. tempDist value comes from a form with a drop down

Something like this?

function dropDownAction(){
    var tempDist=document.HDForm.HuntingDistrict.value;
    var tempDesc=xmlDoc.getElementsByTagName("DISTRICT")[tempDist].getAttribute("LegalDesc");
    document.getElementById("field2").innerHTML=tempDesc;
}

thank in advance

1
  • is district unique (as in only one row has district 100)? Commented Jan 2, 2012 at 19:32

1 Answer 1

1

Personally, I would load all the rows into an array (using district as a key); then simply get the description from the array:

var xmlDoc = // load doc
var elementsToLoopThru=xmlDoc.getElementsByTagName("row");
var rowArray = new Array();

// go thru all the rows
for (var i = 0;i<elementsToLoopThru.length;i++){
    var district = "";
    var description = "No Description";
    var currRowChildren = elementsToLoopThru[i].childNodes;

    // go thru all the elements of a row and get the district and description
    for (var j = 0;j < currRowChildren.length; j++){
        if (currRowChildren[j].nodeName == "DISTRICT"){
            district = currRowChildren[j].childNodes[0].nodeValue;
        }
        else if (currRowChildren[j].nodeName == "LegalDesc"){
            description = currRowChildren[j].childNodes[0].nodeValue;
        }
    }

    // if district and description found, enter them as key value pair into array
    if (district != ""){
        rowArray[district] = description;
        alert(district + ": " + description); // DELETE ME
    }
}

Double check my code; I think it should work, but I am pretty rusty navigating XML

Then when you need to get value, simply access rowArray[NAME_OF_DISTRICT]

This should work if:

  • Districts are unique
  • and it DOESN'T MATTER IF DISTRICTS ARE ORDERED

here is jsfiddle: http://jsfiddle.net/RwE9s/15/

Sign up to request clarification or add additional context in comments.

4 Comments

After running the above code the rowArray remains with length 0 and never fills in. My xml is actually 162 in length. elementsToLoopThru is 162 in lenght while currRowChildren is only 23 in length. Here is my xml dl.dropbox.com/u/6029868/HdData.xml
Fixed, I forgot that each node has a text node inside which holds the content
I'll try this again. I am now thinking about going the way of JSON to hold my dataset.
Then you are just parsing json, it will be even easier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.