在html页面中显示xml数据

时间:2015-05-15 15:58:14

标签: javascript html xml

我的主页:

<html>
<head>

<script src="read.js" type="text/javascript"></script>
</head>
<body onload="process()">


    <div id="theD" />





</body>

</html>

和xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<CategoriesRoot>
    <Categories>
        <CategoryID>1</CategoryID>
        <CategoryName>Beverages</CategoryName>
        <Description>Soft drinks, coffees, teas, beer, and ale</Description>
    </Categories>
    <Categories>
        <CategoryID>2</CategoryID>
        <CategoryName>Condiments</CategoryName>
        <Description>Sweet and savory sauces, relishes, spreads, and seasonings</Description>
    </Categories>
    <Categories>
        <CategoryID>3</CategoryID>
        <CategoryName>Confections</CategoryName>
        <Description>Desserts, candies, sweetbreads</Description>
    </Categories>
    <Categories>
        <CategoryID>4</CategoryID>
        <CategoryName>Dairy Products</CategoryName>
        <Description>Cheeses</Description>
    </Categories>
    <Categories>
        <CategoryID>5</CategoryID>
        <CategoryName>Grains/Cereals</CategoryName>
        <Description>Breads, crackers, pasta, and cereal</Description>
    </Categories>
    <Categories>
        <CategoryID>6</CategoryID>
        <CategoryName>Meat/Poultry</CategoryName>
        <Description>Prepared meats</Description>
    </Categories>
    <Categories>
        <CategoryID>7</CategoryID>
        <CategoryName>Produce</CategoryName>
        <Description>Dried fruit and bean curd</Description>
    </Categories>
    <Categories>
        <CategoryID>8</CategoryID>
        <CategoryName>Seafood</CategoryName>
        <Description>Seaweed and fish</Description>
    </Categories>
</CategoriesRoot>

和js文件:

function process()
{
var xmlhttp;

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    names=xmlhttp.responseXML.documentElement.getElementsByTagName("CategoryName");
    var stuff ;
    for (var i=0;i<names.length;i++)
      {
        stuff = names.item(i).firstChild.data + "<br/>";

      }
         theD = document.getElementById("theD");
         theD.innerHTML = stuff;

    }
  }
xmlhttp.open("GET","categories.xml",true);
xmlhttp.send();
}

问题是当我运行主页时,它只是在屏幕上显示海鲜。 我找不到我弄错的地方。我需要显示所有类别名称。 请帮忙!!非常感谢你!!

0 个答案:

没有答案