0

下面发布的功能是解析 XML 文件并显示标签名称“员工”。但是当我在网络浏览器(Chrome)中加载包含以下发布功能的 .HTML 文件时,该功能不显示变量“x”的内容,我不知道该代码中的错误是什么。

请举例说明如何显示 XML 文件的内容。

.html 文件

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">   
  <link rel="stylesheet" href="readXML.css" type="text/css">
     <title>Read First Child</title>

        <!-- <xml ID="Schriftsteller" SRC="D:\files\files\Schriftsteller.xml"></xml> -->

           <script language="JavaScript">

           var dname = "D:\files\files\xml.xml";
              function findWriter() 
              {
                 var xmlDoc;
                    if (window.XMLHttpRequest)
                    {
                      xmlDoc=new window.XMLHttpRequest();
                      xmlDoc.open("GET",dname,false);
                      xmlDoc.send("");
                         return xmlDoc.responseXML;
                    } 
                 // IE 5 and IE 6
                    else if (ActiveXObject("Microsoft.XMLDOM"))
                    {
                      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                      xmlDoc.async=false;
                      xmlDoc.load(dname);
                         return xmlDoc;
                    }
                         alert("Error loading document!");
                        return null;

                 var  x, xmlDOC;

                 xmlDoc = loadXMLDoc("D:\files\files\xml.xml");
                 x = xmlDoc.documentElement.getElementsByTagName("employee");
                 document.Write(x);
              }
           </script>
    </head>
   <body onload="loadXMLDoc(dname)">
   <span ID="blueBack">Read firstChild</span>
   <div>
   <form name="show"
   <input type=text name="me">
   <input type="button" value="Display Writer"
   onClick="findWriter()">
   </form>
  </div>

xml.xml

< ?xml version="1.0" encoding="UTF-8" ?>
<company>
    <employee id="001" >John</employee>
    <turnover>
        <year id="2000">100,000</year>
        <year id="2001">140,000</year>
        <year id="2002">200,000</year>
    </turnover>
</company>
4

2 回答 2

0

我认为您的问题是您试图从 XML 中获取员工信息。您应该更改设置 x 的行,如下所示:

x = xmlDoc.getElementsByTagName("employee");

更新:

抱歉-我应该更深入地研究您的代码。我发现了许多问题/错误,但幸运的是,我最近不得不做一些 XML 解析,所以我在下面有你想要的基础知识:

<html>
   <head>
      <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
      <meta content="utf-8" http-equiv="encoding">   
      <link rel="stylesheet" href="readXML.css" type="text/css">
      <title>Read First Child</title>

      <!-- <xml ID="Schriftsteller" SRC="D:\files\files\Schriftsteller.xml"></xml> -->

      <script language="JavaScript">

         var dname = "D:\files\files\xml.xml";
         var xmlDoc;
         if (window.XMLHttpRequest) {
         xmlDoc = new XMLHttpRequest();
         } else {
         // IE 5/6
         xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
         }
         function findWriter() {
            xmlDoc.open("GET",dname,false);
            xmlDoc.send();
            var xmlResponse = xmlDoc.responseXML;
            var first = xmlResponse.getElementsByTagName("employee")[0];
            var name, id;
            if(typeof(first.textContent) != 'undefined')
               name = first.textContent;
            else if(typeof(first.text) != 'undefined')
               name = first.text; // IE
            id = first.getAttribute('id');
            //console.log('Employee '+name+', ID '+id);
            document.write('Employee '+name+', ID '+id+'<br>');
            var years = xmlResponse.getElementsByTagName("year");
            var year, sales;
            for(var i=0; i<years.length; i++) {
               year = years[i].getAttribute('id');
               if(typeof(years[i].textContent) != 'undefined')
                  sales = years[i].textContent;
               else if(typeof(years[i].text) != 'undefined')
                  sales = years[i].text; // IE
               //console.log('year '+year+' sales='+sales);
               document.write('year '+year+' sales='+sales+'<br>');
            }
            document.close();
         }
      </script>
   </head>
   <body>
      <span ID="blueBack">Read firstChild</span>
      <div>
         <form name="show">
            <input type=text name="me">
            <input type="button" value="Display Writer"
               onClick="findWriter()">
         </form>
      </div>
   </body>
</html>

差点忘了:

您的 XML 示例有一个小错误;更改第一行:

<?xml version="1.0" encoding="UTF-8" ?>
于 2013-07-24T14:09:13.353 回答
-1

我没有看到很多回调。所以我的猜测是让你的代码更加异步可以帮助......但这里有一个教程如何用 jQuery 做这样的事情(著名的 Javascript 库有很大帮助)

http://think2loud.com/224-reading-xml-with-jquery/

于 2013-07-24T14:09:54.603 回答