首页 > 解决方案 > jQuery - 如果子标签和父标签具有相同的名称,如何读取 xml 数据

问题描述

我无法从 XML 文件中获取数据,因为父节点和子节点具有相同的名称。

XML 代码:文件名restaurant_reviews.xml

<restaurants>
  <restaurant>
    <name>Laughing Man</name>
    <logo>
      <name>Visit the Laughing Man</name>
      <imagefile>laughing-man.gif</imagefile>
      <width unit="px">50</width>
      <height unit="px">200</height>
    </logo>
    <location>
      <street>2100 Lakeview Road</street>
      <city>Ottawa</city>
      <provstate>ON</provstate>
      <postalzipcode>K2K 2K2</postalzipcode>
    </location>
    <phone>613-123-4567</phone>
    <rating>4</rating>
    <date>2020-01-07</date>
    <reviewer>Hunter S. Thompson</reviewer>
    <summary>Laughing Man Inn is a completely restored Victorian hotel, and offers a warm and elegant atmosphere and gracious service.</summary>
    <appetizers>
      <menuitem>
        <description>Crab cakes with a creamy dill sauce served with crispy herbed fries.</description>
        <price currency="cad">23</price>
      </menuitem>
      <menuitem>
        <description>Garden vegetable soup with chocolate sauce.</description>
        <price currency="cad">15</price>
      </menuitem>
    </appetizers>
    <entrees>
      <menuitem>
        <description>Braised jumbo sea scallops on a sun-dried roast tomato base.</description>
        <price currency="cad">39</price>
      </menuitem>
      <menuitem>
        <description>Free-range chicken breast stuffed with morels in a brandy sauce.</description>
        <price currency="cad">31</price>
      </menuitem>
      <menuitem>
        <description>Roast pork loin on a bed of garlic ginger apple slaw.</description>
        <price currency="cad">34</price>
      </menuitem>
    </entrees>
  </restaurant>
  <restaurant>
    <name>Gong&#x2019;s Asian Cuisine</name>
    <logo>
      <name/>
      <imagefile>gong-asian-cuisine.gif</imagefile>
      <width unit="px">150</width>
      <height unit="px">250</height>
    </logo>
    <location>
      <street>1385 Woodroffe Avenue</street>
      <city>Ottawa</city>
      <provstate>ON</provstate>
      <postalzipcode>K2G 1V8</postalzipcode>
    </location>
    <phone>613-727-4723</phone>
    <rating>3</rating>
    <date>2020-01-10</date>
    <reviewer>Brenda P. Smith</reviewer>
    <summary>The food is always consistent. A good variety of dim sum, sushi, Chinese and even Vietnamese dishes. The service is quick and the food is plentiful. We sat down last night and within seconds of turning in our first order, the plates were on the table! The teriyaki dishes had the right amount of sauce and weren't sickeningly sweet. The chicken, fish, and beef were perfectly cooked. The maki-rolls were neatly rolled, cut properly and seasoned well. Loved the General Tao chicken, the steamed BBQ pork buns, the eel sushi and the lovely eggplant. We will be going back again! I would highly recommend it.</summary>
    <appetizers>
      <menuitem>
        <description>Egg Roll</description>
        <price currency="cad" quantity="12">15</price>
        <price currency="cad">1.5</price>
      </menuitem>
      <menuitem>
        <description>Spring Roll</description>
        <price currency="cad" quantity="12">15</price>
        <price currency="cad">1.5</price>
      </menuitem>
      <menuitem>
        <description>Shrimp Chips</description>
        <price currency="cad">2.5</price>
      </menuitem>
      <menuitem>
        <description>Fried Noodles</description>
        <price currency="cad">1</price>
      </menuitem>
      <menuitem>
        <description>Sweet &amp; Sour Fried Wonton Skin</description>
        <price currency="cad" quantity="12">3.50</price>
      </menuitem>
    </appetizers>
    <entrees>
      <menuitem>
        <description>Scallops with Vegetables</description>
        <price currency="cad">14.95</price>
      </menuitem>
      <menuitem>
        <description>Sweet and Sour Fish Cantonese Style</description>
        <price currency="cad">13.95</price>
      </menuitem>
      <menuitem>
        <description>Shrimps with Vegetables</description>
        <price currency="cad">12.95</price>
      </menuitem>
      <menuitem>
        <description>Beef with Mushroom</description>
        <price currency="cad">10.25</price>
      </menuitem>
      <menuitem>
        <description>Beef with Tomato</description>
        <price currency="cad">10.25</price>
      </menuitem>
      <menuitem>
        <description>Beef with Broccoli</description>
        <price currency="cad">10.25</price>
      </menuitem>
    </entrees>
  </restaurant>
</restaurants>

现在要检索我使用以下代码的数据:

$.ajax({
            type: "GET",
            url: "./restaurant_reviews.xml",
            dataType: "xml",
            success: function(xml){
                $(xml).find("restaurant").each(function () {
                    alert($(this).find("name").text());
                    if($(this).find("name").text()=='Laughing Man'){
                        $(this).find("location").each(function () {
                            $("input[name=txtStreetAddress]").val($(this).find("street").text());
                        });
                    }
                 });
            }
        });
当我运行此代码时,名称变为Laughing ManVisit the Laughing Man,这意味着它同时返回父节点和子节点数据。

任何人都可以帮忙吗?

标签: jqueryxml

解决方案


使用选择器而不是find

$(xml).find("restaurant").each(function () {
                    alert($(this).children("name").text());
                    if($(this).children("name").text()=='Laughing Man'){
                        $(this).find("location").each(function () {
                            $("input[name=txtStreetAddress]").val($(this).find("street").text());
                        });
                    }
                 });

演示在这里


推荐阅读