xml文档:books.xml<bookstore>
<book category="children">
<title lang="en">sky</title>
<author></author>
</book>
</bookstore>js文档:
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body><script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
document.write("title="+txt);y=xmlDoc.getElementsByTagName("author")[0].childNodes[0];
txt1=y.nodeValue;
document.write("author="+txt1);
</script>
</body>
</html>遍历节点的时候可以得到 title=sky
为何得不到author= 
是不是因为author节点的值为空,我想得到author=  如何编写程序呢?
附:
loadxmldoc.js文档:function loadXMLDoc(dname) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return(xmlDoc);
  }
catch(e) {alert(e.message)}
return(null);
}

解决方案 »

  1.   

    xml也是dom,直接用jquery操作,可以方便点
      

  2.   

    试试xml改成<bookstore>
    <book category="children">
    <title lang="en">sky</title>
    <author/>
    </book>
    </bookstore>
      

  3.   

    <author></author>为空,所以y=xmlDoc.getElementsByTagName("author")[0].childNodes[0]为空
    判断一下
    txt1= y==null ? "" : y.nodeValue;
      

  4.   

    你的<author></author>不能为空,因为一旦为空之后,代码就会出错,而且也没有异常处理,加上判断可以,也可以用一个特殊字符来表示为空比如*号,一般不建议为空,很可能会影响解析