2014-10-17 87 views
-1

对于HTML,JavaScript和XML(在前两个方面)我很新,所以记住这一点。我有下面的XML文档:document.getElementsByTagName()返回错误的元素

<?xml version="1.0" encoding="UTF-8"?> 
<university 
    xmlns="myNS:university" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="myNS university.xsd"> 
    <students> 
     <student sid="jdoe12345> 
      <lastname>Doe</lastname> 
      <firstname>John</lastname> 
      <courses> 
       <course cid="MB100"> 
        <grade attempt="1" term="W10" type="simple">5.0</grade> 
        <grade attempt="2" term="S11" type="simple">2.3</grade> 
       </course> 
       <course cid="MB110"> 
        <grade attempt="1" term="S11" type="complex">2.0</grade> 
        <course cid="MB111"> 
         <grade attempt="1" term="S11" type="simple">1.0</grade> 
        </course> 
        <course cid="MB112"> 
         <grade attempt="1" term="S11" type="simple">5.0</grade> 
         <grade attempt="2" term="S11" type="simple">3.0</grade> 
        </course> 
       </course> 
       ... 
      </courses> 
     </student> 
     ... 
    </students> 
</university> 

我会尽力解释的结构是什么尽可能短:顶部元素是“大学”,它包含一个孩子的“学生”。另一方面,“学生”可以包含多个“学生”元素,每个元素具有独特的学生标识“sid”作为属性,“姓氏”元素,“名字”元素和包含所有课程的元素“课程” “course”元素是“学生参加的”,“course”元素也有一个唯一的标识,它可以代表单个课程(上例:MB111,MB112)或模块(上例:MB100,MB110)。 “课程”包含的是“成绩”元素,可以是“简单”和“复杂”两种类型。多次尝试可用,每次尝试都会评分(如上例:对于MB100,我们有两次尝试)。简单等级用于由单一课程组成的模块(上例:MB100),另一方面复杂等级是每个课程的所有简单等级的算术平均值,一个模块包含所有尝试的最高等级单个课程(上面的例子:MB110有一个等级= 2.0,它是1.0和3.0的算术平均值,对于每个课程而言,其中3.0代表MB112课程中成绩最高的课程)。

这就是说我想创建一个HTML页面并使用JavaScript为其中的所有学生生成一个表格。目前,我正在尝试显示每个“学生”元素的“sid”属性以及他/她的名字和姓氏。我的页面源代码如下:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>My pretty page</title> 
     <link rel="stylesheet" type="text/css" href="my_stylesheet.css"/> 
    </head> 
    <body> 
     <script type="text/javascript"> 
      xmlhttp = new XMLHttpRequest(); 
      xmlhttp.open("GET","/home/USERNAME/xmlplayground/university.xml",false); 
      xmlhttp.send(); 
      xmlDoc = xmlhttp.responseXML; 
      document.write("<table border='1'>"); 
      var x = xmlDoc.getElementsByTagName("*","student"); 
      for(i = 0; i < x.length; i++) { 
       document.write("<tr><td>"); 
       document.write(x[i].getAttribute("sid")); 
       document.write("</td><td>"); 
       document.write(x[i].getElementsByTagName("*","lastname")[0].childNodes[0].nodeValue); 
       document.write("</td><td>"); 
       document.write(x[i].getElementsByTagName("*","firstname")[0].childNodes[0].nodeValue); 
       document.write("</td></tr>"); 
      } 
      document.write("</table>"); 
     </script> 
    </body> 
</html> 

出于某种原因,我得到一个非常奇怪的输出:

+--------------------------------------------------------------------------------------------------------------------------------------+ 
| null | 5.0 --------------------------------------------------------^ | 5.0 --------------------------------------------------------^ | 
+--------------------------------------------------------------------------------------------------------------------------------------+ 
| null |     EMPTY CELL HERE!!!       |      NO CELL HERE!!! 
+----------------------------------------------------------------------+ 

(在Debian Firefox)的调试器使用Iceweasel的我已经注意到,X变量有2个这两个孩子给一个innerHTML的错误:

"XML Parsing Error: not well-formed 
Location: file:///home/USERNAME/xmlplayground/university.xml 
Line Number 12, Column 57: 
<sourcetext xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml"> 
        &lt;grade attempt="1" term="WS10" type=simple&gt;5.0&lt;/grade&gt;--------------------------------------------------------^ 
</sourcetext>" 

我也不得不提,我使用氧XML编辑器,它至今已被证明是非常错误(与XSD,DTD工作和XSLT不提到它几乎不存在对JavaScript编辑的支持),但我或多或少地被迫使用它。虽然这个问题看起来与我亲自写的东西更相关。

+4

的'.getElementsByTagName()'函数只需要1个参数。 – Pointy 2014-10-17 14:24:35

+0

你说得对。虽然我什么也没有得到,但当我将它移开时。调试器表示x的长度为0.: -/ – rbaleksandar 2014-10-17 14:27:45

+1

Hm,' 5.0 * *格式不正确(缺少“type”属性的值)。 – Bergi 2014-10-17 14:27:55

回答

1

哈哈,您已关闭您的firstname标记错误,应该用</firstname>而不是</lastname>关闭。 :P

应用此JS:

xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", "/home/USERNAME/xmlplayground/university.xml", false); 
xmlhttp.send(); 
xmlDoc = xmlhttp.responseXML; 
document.write("<table border='1'>"); 
var x = xmlDoc.getElementsByTagName("student"); 
for (var i = 0; i < x.length; i++) { 
    document.write("<tr><td>"); 
    document.write(x[i].getAttribute("sid")); 
    document.write("</td><td>"); 
    document.write(x[i].getElementsByTagName("lastname")[0].childNodes[0].nodeValue); 
    document.write("</td><td>"); 
    document.write(x[i].getElementsByTagName("firstname")[0].childNodes[0].nodeValue); 
    document.write("</td></tr>"); 
} 
document.write("</table>"); 

而且这里是你的固定XML:

<?xml version="1.0" encoding="UTF-8"?> 
<university 
    xmlns="myNS:university" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="myNS university.xsd"> 
    <students> 
     <student sid="jdoe12345"> 
      <lastname>Doe</lastname> 
      <firstname>John</firstname> 
      <courses> 
       <course cid="MB100"> 
        <grade attempt="1" term="W10" type="simple">5.0</grade> 
        <grade attempt="2" term="S11" type="simple">2.3</grade> 
       </course> 
       <course cid="MB110"> 
        <grade attempt="1" term="S11" type="complex">2.0</grade> 
        <course cid="MB111"> 
         <grade attempt="1" term="S11" type="simple">1.0</grade> 
        </course> 
        <course cid="MB112"> 
         <grade attempt="1" term="S11" type="simple">5.0</grade> 
         <grade attempt="2" term="S11" type="simple">3.0</grade> 
        </course> 
       </course> 
      </courses> 
     </student> 
    </students> 
</university> 
+0

我按照@Pointy的建议在我的原始文档中更改了此项。在我的问题下面看到我的评论。 – rbaleksandar 2014-10-17 14:34:36

+0

现在在控制台中的任何消息? – 2014-10-17 14:35:15

+0

是的,在做了一些修改之后,似乎我的XML格式不正确。在控制台中显示链接到我的university.xml文件的消息“格式不正确”。我会检查它,看看问题出现在哪里。氧XML似乎给我一些关于我的XML文件的特别关于“课程”和“cid”的非常混淆的错误消息。一方面它说“属性'{myNS:university} cid'必须出现在元素'course''上,但另一方面它说'属性'cid'不允许出现在元素'course'中”??? – rbaleksandar 2014-10-17 14:43:44