做个试验,下面一段xml数据。
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Go Go Go</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
  <gogogo>i love you!</gogogo>
</bookstore>
ie 用 ActiveXObject,
firefox,opera用document.implementation.createDocument 获得DOM对象。现在提取第一个 'title'元素中的文本值。采用: document.getElementById("Textarea1").value = oXmlDom.getElementsByTagName("book")[0].childNodes[0].childNodes[0].nodeValue;IE可以正确提取出来,放到文本区控件中,显示 'Go Go Go' , 而firefox和opera报错,总之意思就是对象是个 null。换一种写法: document.getElementById("Textarea1").value = oXmlDom.getElementsByTagName("title")[0].childNodes[0].nodeValue;ie ,firefox,opera 都可以正确显示。貌似使用 连点 '.' 的时候firefox,opera识别能力差一些。
safari 5.0.1 和 谷歌浏览器 暂没有测试,load方法不能用。大家是怎么看待的?

解决方案 »

  1.   

    喜欢用ff和chrome,往往很多错误在IE8下会出现。
      

  2.   

    Firefox 和Opera中 childNodes[0] 应该会取到一个 TEXT 节点,因为你的XML文件里有回车和空格
      

  3.   

    终于搞明白了,原来 ie 和 firefox ,opera 这有一个差别,就是正如 ‘WebAdvocate’说的,firefox和opera 会取到空格这样的text ,我测试了一下,确实如此。
                var textAreaValue = "";
                var book = oXmlDom.getElementsByTagName("book")[0];
                var bookLength = book.childNodes.length;
                alert(bookLength);
                var first = book.firstChild;
                for (var i = 0; i < bookLength; ++i) {
                    alert(first.nodeType);
                    if (first.nodeType == 1) {
                        textAreaValue += first.childNodes[0].nodeValue + "\n";
                    }
                    else if (first.nodeType == 3) {
                        textAreaValue += "this is a null text" + "\n";
                    }
                    first = first.nextSibling;
                }
                document.getElementById("Textarea1").value = textAreaValue;
    显示结果:this is a null text
    Go Go Go
    this is a null text
    Giada De Laurentiis
    this is a null text
    2005
    this is a null text
    30.00
    this is a null text原来如此,以后要注意了。