我试图弄清楚如何获取当前父元素的 XML 子元素。现在,如果我尝试获取当前的子元素,如果它不存在,我只会获取具有此名称的下一个元素,但结果为空...
我已经尝试获取此父元素的所有子元素,但没有找到任何方法来执行此操作...
目前,我的代码如下所示:
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>";
我所指的 XML,或者至少它的一部分看起来像这样:
<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>
如您所见,只有第二个位置有<house_number>标签。如果我将我的代码与这个 XML 文件一起使用,我会得到第一个元素的门牌号 63,而第二个元素没有门牌号。这就像如果父 XML 不包含“house_number”元素,它只会选择它找到的下一个元素 - 稍后会出现一些父元素......
我希望我解释得足够清楚,我希望它没有重复,但我没有找到任何东西,我真的不知道我自己怎么能做到这一点......
提前致谢
尼可