...

View Full Version : get siblings



paulinho
03-15-2011, 05:03 PM
Hi!
I need some help trying to get siblings in an xml file, I have got the value of the element <name> and want to find the value of names siblings. The problem is that there are a lot of name elements, but since I have the specific value of name i Should be able to find its siblings, shouldn't I?

example of the xml-file that I am parsing:


<Layer queryable="0" opaque="0" cascaded="0">
<Name>na_drain_nam</Name>
<Title>River Names</Title>
<Abstract>Names of rivers in the North America region.</Abstract>
<SRS>EPSG:4326</SRS>
<LatLonBoundingBox minx="-180" miny="-4.94415" maxx="180" maxy="89.9992"/>
<BoundingBox SRS="EPSG:4326" minx="-180" miny="-4.94415" maxx="180" maxy="89.9992"/>
</Layer>

<Layer queryable="0" opaque="0" cascaded="0">
<Name>na_division_nam</Name>
<Title>Province, Territory, State Names</Title>

<Abstract>
Province, territory, and state names in the North America region.
</Abstract>
<SRS>EPSG:4326</SRS>
<LatLonBoundingBox minx="-180" miny="-4.94415" maxx="180" maxy="89.9992"/>
<BoundingBox SRS="EPSG:4326" minx="-180" miny="-4.94415" maxx="180" maxy="89.9992"/>
</Layer>

How or what function in javascript can I use to get the values for the siblings?

please help!
/Paul

Kor
03-17-2011, 12:55 PM
Could be something like this:


var val='na_drain_nam';
var names=document.getElementsByTagName('Name'), n, i=0, children, c, res=[], j;
while(n=names[i++]){
if(n.textContent==val||n.text==val){
alert(n.nodeName)
children=n.parentNode.childNodes;
j=0;
while(c=children[j++]){
if(c!=n&&c.nodeType==1){
res[res.length]=c.textContent||c.text;
}
}
break
}
}
alert(res)
}

paulinho
03-21-2011, 11:11 AM
Thanks a lot! with some changes in your code I got it to work perfekt in my work, thank you!
ps. United rules!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum