I'm trying to figure out how I can get the XML child element of the current parent element. Right now if I try to get the current child element, if it doesn't exist I just get the next element with this name except an empty result...
I already tried to get all children of this parent element but didn't find any way how to do this...
Currently, my code looks like this:
x = xmlDoc.getElementsByTagName('place');
for (i = 0; i < (x.length - 1) ;) {
type = x[i].getAttribute('type');
console.warn("Typ: " + type);
xname = xmlDoc.getElementsByTagName(type);
name = xname[i].childNodes[0].nodeValue;
txt += name + "<br>";
xroad = xmlDoc.getElementsByTagName("road");
road = xroad[i].childNodes[0].nodeValue;
txt += road + " ";
xnum = xmlDoc.querySelectorAll("house_number");
num = xnum[i].childNodes[0].nodeValue;
txt += num + "<br>";
The XML I'm referring to, or at least parts of it look like this:
<place place_id="57293627" osm_type="node" osm_id="4605575366" place_rank="30" boundingbox="48.8344591,48.8345591,8.2877028,8.2878028" lat="48.8345091" lon="8.2877528" display_name="Rheinau-Bäck, Murgtalstraße, Bischweier, Nachbarschaftsverband Bischweier-Kuppenheim, Landkreis Rastatt, Regierungsbezirk Karlsruhe, Baden-Württemberg, 76476, Deutschland" class="shop" type="bakery" importance="0.001" icon="https://nominatim.openstreetmap.org/images/mapicons/shopping_bakery.p.20.png">
<extratags>
<tag key="opening_hours" value="Mo-Sa 06:00-20:00"/>
</extratags>
<bakery>Rheinau-Bäck</bakery>
<road>Murgtalstraße</road>
<village>Bischweier</village>
<county>Nachbarschaftsverband Bischweier-Kuppenheim</county>
<state_district>Regierungsbezirk Karlsruhe</state_district>
<state>Baden-Württemberg</state>
<postcode>76476</postcode>
<country>Deutschland</country>
<country_code>de</country_code>
</place>
<place place_id="239017" osm_type="node" osm_id="52623297" place_rank="30" boundingbox="48.9310367,48.9311367,8.2681663,8.2682663" lat="48.9310867" lon="8.2682163" display_name="Maier Bäck, 63, Hauptstraße, Durmersheim, Verwaltungsverband Durmersheim, Landkreis Rastatt, Regierungsbezirk Karlsruhe, Baden-Württemberg, 76448, Deutschland" class="shop" type="bakery" importance="0.001" icon="https://nominatim.openstreetmap.org/images/mapicons/shopping_bakery.p.20.png">
<extratags>
<tag key="wheelchair" value="yes"/>
<tag key="contact:phone" value="+49 7245 2338"/>
</extratags>
<bakery>Maier Bäck</bakery>
<house_number>63</house_number>
<road>Hauptstraße</road>
<town>Durmersheim</town>
<county>Verwaltungsverband Durmersheim</county>
<state_district>Regierungsbezirk Karlsruhe</state_district>
<state>Baden-Württemberg</state>
<postcode>76448</postcode>
<country>Deutschland</country>
<country_code>de</country_code>
</place>
As you can see, only the second place has a <house_number>
tag. If I would use my code with this XML file, I would get the house number 63 for the first element and no house number for the second element. It's like if the parent XML doesn't contain a "house_number" element, it just picks the next one it finds - some parent elements later...
I hope I explained it clear enough and I hope it's no duplicate but I didn't find anything and I have literally no idea how I could do this by myself...
Thanks in advance
Niko
The basic problem is you query the whole document inside your loop for child tags of <place>
Therefore the indexing of those full document queries won't match the indexing of <place>
used in your loop since there could be more or less of the nested tags
Instead, query within each place
instance so a query like:
xnum = xmlDoc.querySelectorAll("house_number");
Would be more like
xnum = x[i].querySelector("house_number");
if(xnum ){
num = xnum.childNodes[0].nodeValue;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.