Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Nodes of XML file.

Status
Not open for further replies.

dpalmond

Programmer
Jul 3, 2007
5
US
How can I populate a select list with the Facilities listed in the following xml file:

<HealthServices>
<Divisions>
<Division>Home Office</Division>
<Facility>Administration</Facility>
<Facility>Finance</Facility>
<Facility>Human Resources</Facility>
<Facility>Operations</Facility>
</Divisions>
<Divisions>
<Division>Hospital</Division>
<Facility>Facility1</Facility>
<Facility>Facility2</Facility>
</Divisions>
<Divisions>
<Division>Sr. Housing</Division>
<Facility>Facility3</Facility>
<Facility>Facility4</Facility>
</Divisions>
</HealthServices>

I have one select list populated with the divisions using the following function:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
//var allRoleListing;
var allDivisions;
var allDivisionNodes;
var allFacilityNodes;

function init(){


xmlDoc.async = false;
xmlDoc.load("./xml/esf_data4.xml");

//allRoleListing = xmlDoc.selectNodes("/IndividualListing");
allDivisions = xmlDoc.selectNodes("//Divisions");
allDivisionNodes = xmlDoc.selectNodes("//Division");
allFacilityNodes = xmlDoc.selectNodes("//Facility");

populateDivisionList();
populateFacilityList();
}

function populateDivisionList(){
clearSelect('OBDataset_Division_1');

var lstDivision = document.getElementById('OBDataset_Division_1');

for (var i=0; i<allDivisions.length; i++) {
var thisDivisionNameTxt = allDivisions.selectNodes("./Division")[0].text;
var newOption = document.createElement("option");
newOption.innerHTML=thisDivisionNameTxt;
newOption.value = allDivisions.selectNodes("./Division")[0].text;

lstDivision.appendChild(newOption);
}
document.getElementById('nTest2').innerHTML=allFacilityNodes.length;
}

This gets me all the facilities in the first Division node, but errors because the length of allFacilityNodes counts all the facility nodes in the file and I just want to count the ones in the first Division node or second,ect., depending what was selected in the Division list. Does anyone know how to fix this?
 
This was the second function to populate the second select list.

function populateFacilityList(){
clearSelect('OBDataset_Facility_1');

var lstDivision = document.getElementById('OBDataset_Facility_1');

for (var i=0; i<allFacilityNodes.length; i++) {
var thisDivisionNameTxt = allDivisions[0].selectNodes("./Facility").text;
var newOption = document.createElement("option");
newOption.innerHTML=thisDivisionNameTxt;
newOption.value = allDivisions[0].selectNodes("./Facility").text;

lstDivision.appendChild(newOption);
}
}

 
After you load up your XMLDoc here:

Code:
xmlDoc.load("./xml/esf_data4.xml");

Use
Code:
var facilityList = xmlDoc.getElementsByTagName("Facility");

From there you can loop through each Facility object and built the dropdown dynamically, grabbing the text of each Facility with

facilityList[a].text

where a is a variable in a for loop.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top