簡體   English   中英

我如何僅獲取XML子元素(如果存在),除了獲取下一個父元素的子元素之外?

[英]How can I only get the XML child element if it exists except getting the child of the next parent?

我試圖弄清楚如何獲取當前父元素的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”元素一樣,它只選擇找到的下一個-稍后有一些父元素...

我希望我已經解釋得足夠清楚了,我希望它不會重復,但是我什么也沒找到,而且我根本不知道我自己該怎么做...

提前致謝

尼可

基本問題是您在循環內的整個文檔中查詢<place>子標記

因此,這些完整文檔查詢的索引與循環中使用的<place>的索引不匹配,因為可能存在更多或更少的嵌套標簽

而是在每個place實例中查詢,這樣的查詢如下:

xnum = xmlDoc.querySelectorAll("house_number");

會更像

xnum = x[i].querySelector("house_number");
if(xnum ){
    num = xnum.childNodes[0].nodeValue;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM