使用外部XML文件在JavaScript中解析xml

时间:2019-03-19 16:47:05

标签: javascript html xml

我正在尝试使用JavaScript解析XML文档,但是收到TypeError:xmlDoc.getElementsByTagName(...)[0]未定义。我想确保我正确解析了XML。

这是我加载外部XML的功能:

function loadXML(callback) {   

        var xobj = new XMLHttpRequest();
            xobj.overrideMimeType("application/xml");
        xobj.open('GET', 'restaurant_menu.xml', true); 
        xobj.onreadystatechange = function () {
              if (xobj.readyState == 4 && xobj.status == "200") {
                // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
                callback(xobj.responseText);
              }
        };
        xobj.send(null);  
}

然后我要在此JavaScript函数中加载XML解析:

function showBreakfast() {

  loadXML(function(response) {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(response, "text/xml");

        document.getElementById("breakfast").innerHTML = xmlDoc.getElementsByTagName("Name")[0].childNodes[0].nodeValue;
    });

}

但是我收到一个TypeError。我可以正确解析XML吗?这是我的XML文档。我的演出早餐功能不应该收到“水果碗”吗?

<?xml version="1.0" encoding="UTF-8"?>
<MENU>
  <BREAKFAST>
    <APPETIZER><NAME>Fruit Bowl</NAME><PRICE>10.90</PRICE></APPETIZER>
    <SALAD><NAME>Caeser SALAD</NAME><PRICE>3.90</PRICE></SALAD>
    <SOUP><NAME>Chicken Noodle</NAME><PRICE>5.90</PRICE></SOUP>
    <ANTREE><NAME>Omelet</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Scrambled Eggs</NAME><PRICE>20.90</PRICE></ANTREE>
    <DESSERT><NAME>Apple Pie</NAME><PRICE>7.90</PRICE></DESSERT>
  </BREAKFAST>
  <BRUNCH>
    <APPETIZER><NAME>Fruit Bowl</NAME><PRICE>10.90</PRICE></APPETIZER>
    <APPETIZER><NAME>EggPlant Frits</NAME><PRICE>10.90</PRICE></APPETIZER>
    <SALAD><NAME>House Salad</NAME><PRICE>3.90</PRICE></SALAD>
    <SOUP><NAME>Chicken Noodle</NAME><PRICE>5.90</PRICE></SOUP>
    <ANTREE><NAME>French Toast</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Omelet</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Scrambled Eggs</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Hamburg</NAME><PRICE>20.90</PRICE></ANTREE>
    <DESSERT><NAME>Apple Pie</NAME><PRICE>7.90</PRICE></DESSERT>
  </BRUNCH>
  <LUNCH>
    <APPETIZER><NAME>Calamari</NAME><PRICE>10.90</PRICE></APPETIZER>
    <SALAD>Caeser Salad<PRICE>3.90</PRICE></SALAD>
    <SALAD><NAME>Arugula Salad</NAME><PRICE>3.90</PRICE></SALAD>
    <SOUP><NAME>Chicken Noodle</NAME><PRICE>5.90</PRICE></SOUP>
    <SOUP><NAME>Italian Wedding Soup</NAME><PRICE>5.90</PRICE></SOUP>
    <ANTREE><NAME>Steak</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Hamburg</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Angel Hair Pasta</NAME><PRICE>20.90</PRICE></ANTREE>
    <DESSERT><NAME>Tiramisu</NAME><PRICE>7.90</PRICE></DESSERT>
  </LUNCH>
  <DINNER>
    <APPETIZER><NAME>Calamari</NAME><PRICE>10.90</PRICE></APPETIZER>
    <APPETIZER><NAME>EggPlant Frits</NAME><PRICE>10.90</PRICE></APPETIZER>
    <SALAD>House Salad<PRICE>3.90</PRICE></SALAD>
    <SALAD><NAME>Caeser Salad</NAME><PRICE>3.90</PRICE></SALAD>
    <SOUP>Chicken Noodle<PRICE>5.90</PRICE></SOUP>
    <SOUP><NAME>Italian Wedding Soup</NAME><PRICE>5.90</PRICE></SOUP>
    <SOUP><NAME>Roasted Patato</NAME><PRICE>5.90</PRICE></SOUP>
    <ANTREE><NAME>Steak</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Hamburg</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Angel Hair Pasta</NAME><PRICE>20.90</PRICE></ANTREE>
    <ANTREE><NAME>Roasted Duck</NAME><PRICE>20.90</PRICE></ANTREE>
    <DESSERT><NAME>Apple Pie</NAME><PRICE>7.90</PRICE></DESSERT>
    <DESSERT><NAME>Tiramisu</NAME><PRICE>7.90</PRICE></DESSERT>
    <DESSERT><NAME>Creme Brule</NAME><PRICE>7.90</PRICE></DESSERT>
  </DINNER>
  <DRINKS>
    <BEER><NAME>Bud Weiser</NAME><PRICE>5.90</PRICE></BEER>
    <BEER><NAME>Hefe Weissbier</NAME><PRICE>6.90</PRICE></BEER>
    <WINE><NAME>Cabernet Sauvignon</NAME><FROM>Napa Valley</FROM><PRICE>15.90</PRICE></WINE>
    <WINE><NAME>Margaux</NAME><PRICE>50.90</PRICE></WINE>
    <WINE><NAME>Pinot Noir</NAME><FROM>Napa Valley</FROM><PRICE>12.90</PRICE></WINE>
    <WINE><NAME>Chianti</NAME><PRICE>10.90</PRICE></WINE>
    <COFFEE><NAME>Star Bucks</NAME><PRICE>3.90</PRICE></COFFEE>
    <SODA>Coca Cola<PRICE>3.90</PRICE></SODA>
    <SODA>Sprite<PRICE>3.90</PRICE></SODA>
  </DRINKS>
</MENU>

1 个答案:

答案 0 :(得分:1)

您说过xmlDoc.getElementsByTagName("Name"),但是您的XML没有<Name>元素。它返回零长度的节点列表。然后,您尝试读取该列表中第一项的childNodes,但是由于没有该项,因此会出现错误。

XML 确实具有<NAME>元素。 XML区分大小写。