首页 > 解决方案 > 如何使用 FireFox 在 html 中显示 xml?

问题描述

我必须在 HTML 文件的表格中显示 XML 数据。

我尝试关闭 FireFox 上的所有扩展程序,但仍然出现空白屏幕。

员工详情

<script>
        var client;
        if (window.XMLHttpRequest) {
            client = new XMLHttpRequest();
        }
        else {
            client = new ActiveXObject("Microsoft.XMLHTTP");
        }
        client.open('GET', 'employees.xml', false);

        client.onreadystatechange = function() { // will run when the file loads
            // get the response XML
            var xmlDoc = client.responseXML;

            // get the list of user
            var user = xmlDoc.getElementsByTagName("user");

            // get the container where you want to embed the table
            var container = document.getElementById("container");

            var tableString = "<table border='1'>"; // Make a table and put the element data inside it
            for (i = 0; i < user.length; i++) {
                tableString += "<tr><td>";
                tableString += user[i].getElementsByTagName("Firstname")[0].childNodes[0].nodeValue;
                tableString +="</td><td>";
                tableString += user[i].getElementsByTagName("Phone")[0].childNodes[0].nodeValue;
                tableString += "</td></tr>";
            }
            tableString += "</table>";

            // append the table to the container
            container.innerHTML = tableString;
        }

        client.send();

    </script>
<body>
    <h1></h1>
    <div id="container"></div>
</body>

    <?xml version = "1.0" encoding ="UTF-16"?>
    <user_information>
       <user id="1">
          <Firstname> Bobby </Firstname>
          <Lastname>Drake</Lastname>
          <major>Criminal Justice</major>
          <location>Illinois</location>
          <Phone>(923)949-2302</Phone>
       </user>

       <user>
          <Firstname>Scott</Firstname>
          <Lastname>Summers</Lastname>
          <major> </major>
          <location> California </location>
          <Phone>(395)984-7284</Phone>
       </user>

       <user>
          <Firstname>Jean</Firstname>
          <Lastname>Grey</Lastname>
          <major> </major>
          <location>New York</location>
          <Phone>(843)759-6943</Phone>
       </user>

       <user>
          <Firstname>Kevin</Firstname>
          <Lastname>Sydney</Lastname>
          <major> </major>
          <location>New York</location>
          <Phone>(571)-089-4568</Phone>
       </user>

            <user>
              <Firstname>Suzanne</Firstname>
              <Lastname>Chan</Lastname>
              <major> </major>
              <location>Illinois</location>
              <Phone>(685)-583-2849</Phone>
           </user>

</user_information>

在 FireFox 中打开 html 时出现空白屏幕。我不知道这是我的代码还是我电脑上的东西。如果有人可以帮助我,我将不胜感激。

标签: htmlxml

解决方案


我假设您正在尝试使用 file:/// 或从导致 CORS 策略违规的本地目录访问这些文件,并且您的请求被阻止。

您可以使用网络服务器来部署资源并访问它。或者查看相关的线程评论,它可以帮助您使用预期的 CORS 策略运行 Chrome。

跨域请求仅支持 HTTP,但不支持跨域


推荐阅读