2012-01-02 6 views
0

다른 노드의 값을 참조하여 xml 노드의 값을 가져 오려고합니다. 여기 자바 스크립트를 사용하여 XML 쿼리

내가 지구 필드에 #을 기반으로 LegalDesc 필드의 값을 일부 HTML을 작성하고 싶습니다 내 노드

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

중 하나의 조각이다.

예를 들어 DISTRICT==100의 LegalDesc를 표시하고 싶습니다. tempDist 값은 드롭 다운이있는 양식에서 유래합니다.

이와 비슷한 기능이 있습니까?

+0

은 지구가 유일합니다 (한 행에 100 개의 지구가 있음)? – Tomas

답변

1

개인적 미리

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

감사, I는 배열로 모든 행을로드하는 것 (키로서 지구를 사용); 다음 단순히 배열에서 설명을 얻으십시오.

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

내 코드를 다시 확인하십시오. 내가이 일을해야한다고 생각하지만, 난 당신이 가치를 얻을 필요가있을 때

그런 다음, 단순히이 경우 작업을해야 rowArray[NAME_OF_DISTRICT]

에 액세스 꽤 녹슨 항해의 XML을이다 ​​:

  • 지구가 고유
  • 및 지구가 여기

을 ORDERED인지와는 상관없이 것은 jsfiddle입니다 : http://jsfiddle.net/RwE9s/15/

+0

위 코드를 실행 한 후에 rowArray는 길이가 0이고 채우지 않습니다. My XML의 길이는 실제로 162입니다. elementsToLoopThru는 길이가 162이고 currRowChildren은 길이가 23입니다. 여기 내 XML http://dl.dropbox.com/u/6029868/HdData.xml – snowgage

+0

고정, 각 노드가 내용을 보유하고있는 텍스트 노드가 있다는 것을 잊었습니다. – Tomas

+0

다시 시도해 보겠습니다. JSON을 사용하여 데이터 세트를 보관할 생각입니다. – snowgage

관련 문제