繁体   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