SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Traversing the XML tree

    I have the following XML:

    Code:
    <root>
    <MemberProvider  name="myName">
    			
    			<Marker>
    					<Latitude>-32.025469</Latitude>
    					<Longitude>115.950136</Longitude>
    					<Name>Beacon Lighting</Name>
    					<Address>219 21 William Street, Cannington WA 6107</Address>
    					<Phone>08 9356 6422</Phone>
    					<savingText>Members get 10 off RRP</savingText>
    					<disclaimer>Also save 10 off grid connected solar power systems</disclaimer>
    				</Marker>
    			
    			<Marker>
    					<Latitude>-31.692261</Latitude>
    					<Longitude>115.720453</Longitude>
    					<Name>Beacon Lighting</Name>
    					<Address>61 Key Largo Dr, Clarkson WA 6030</Address>
    					<Phone>08 9408 5445</Phone>
    					<savingText>Members get 10 off RRP</savingText>
    					<disclaimer>Also save 10 off grid connected solar power systems</disclaimer>
    				</Marker>
    			</MemberProvider >
    <
    <MemberProvider name="myName">
    </MemberProvider>
    <MemberProvider name="myName">
    </MemberProvider>
    etc.....
    </root>


    Now in my Javascript, I have


    Code:
    var xmlrows = xmlDoc.documentElement.getElementsByTagName("Marker");
    		var xmlGroups = xmlDoc.documentElement.getElementsByTagName("MemberProvider");
    		
    		//for the number of Member Providers listed
    		for (var i = 0; i < xmlrows.length; i++) {
    			var xmlrow = xmlrows[i];
    			var singleGroup = xmlGroups[i];
    				
    				var xmlcellLongitude = xmlrow.getElementsByTagName("Longitude")[0];
    				var xmlcellLatitude = xmlrow.getElementsByTagName("Latitude")[0];
    				
    				var point = new GLatLng(parseFloat(xmlcellLatitude.firstChild.data), parseFloat(xmlcellLongitude.firstChild.data));
    				
    //get the name
    				var name = singleGroup.getElementsByTagName("MemberProvider")[0].getAttribute("name");
    Why do I have two variables loading the xml doc? The first appears to work, but the second (xmlGroups) does not. Every time I try and get the Name attribute from the MemberProvider node, it throws getElementsByTagName("MemberProvider")[0].getAttribute("name"); is null or not an object.

    Can anyone help before I have no hair left!!!!

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The way you have written your code looks strange to me because your looping through the <Marker> elements before you loop through the <MemberProvider> elements. See the below code for how i think it should be written out.

    Code JavaScript:
    var providers = xmlDoc.documentElement.getElementsByTagName('MemberProvider');
     
    // Loop through the "MemberProvider" elements
    for (var i = 0; i < providers.length; i++) {
        var provider = providers[i],
            markers  = provider.getElementsByTagName('Marker'),
            name     = provider.getAttribute('name');
     
        if (markers.length) {
            for (var j = 0; j < markers.length; j++) {
                var marker    = markers[j],
                    longitude = marker.getElementsByTagName('Longitude')[0],
                    latitude  = marker.getElementsByTagName('Latitude')[0],
                    point     = new GLatLng(parseFloat(latitude.firstChild.data), parseFloat(longitude.firstChild.data));
            }
        }
    }
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks SgtLegend, massive help! greatly appreciated.

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No problem
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •