xml - How can I get child elements by element name with JavaScript? -
i have xml file many nodes this:
<map-item> <location-name>u lot (pine sreet)</location-name> <link>https://maps.google.com?daddr=pine+avenue+middletown+ct+06457</link> <latitude>41.5501724</latitude> <longitude>-72.6588056</longitude> <icon-path>img/parking-icon.svg</icon-path> </map-item>
i've looped through of map-item elements in xml file this:
markers = xml.documentelement.getelementsbytagname("map-item"); (var = 0; < markers.length; i++) { //do here each marker }
but i'm having hard time figuring out how drill down , @ child elements inside of each map-item element, such location-name or icon-path. need in order grab values of child elements , them?
you can use getelementsbytagname
again, on each marker. if xml valid , conforms schema, can expect there @ least 1 element of given name in <marker>
.
var markers = xml.documentelement.getelementsbytagname("map-item"); (var = 0; < markers.length; i++) { var marker = markers[i]; var locationname = marker.getelementsbytagname("location-name")[0].textcontent; var iconpath = marker.getelementsbytagname("icon-path")[0].textcontent; … }
if don't know how many child elements there in each <marker>
, you'll have switch on .length
of selected list or loop again.
Comments
Post a Comment