0

我有这样的xml文件

    <bookstore>
      <book category="cooking">
      <title lang="en">Everyday Italian</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" cover="paperback">
      <title lang="en">Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
     </book>
</bookstore>

我想读取这个xml的所有值。即我需要所有节点的值。是否可以在不使用服务器端脚本语言的情况下读取所有节点的值。我用来读取xml所有标题的代码是下面给出。

<script type="text/javascript">
        function loadXMLDoc()
        {
        //var item = document.getElementById('search').value;
        var request = new XMLHttpRequest();
        var url = "http://saletrack.cynere.net/test2.xml";
        request.open("GET",url, true);
        request.send();
        request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200 || request.status == 0) {
                var myxml = request.responseXML;
                var theHTML = '';
                var x = myxml.getElementsByTagName("title");
                for (var i = 0; i < x.length; i++) {
                var y = x[i].childNodes[0].nodeValue;
                theHTML += ['<div class="result"',
                '<div ul="content">',
                '<li><h3>'+y+'</h3></li>',
                '</ul>',
                '</div>'].join('');
                    }
            }
        }
        document.getElementById('list').innerHTML = theHTML;
        }
        }

        document.addEventListener("deviceready", loadXMLDoc, false);

    </script>

提前致谢..........

4

1 回答 1

1

你能用jquery代替核心javascript吗?我正在使用以下代码来显示本地 xml 文件内容。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
//Code Starts
$(document).ready(function(){
  $("#list").append("<ul></ul>");
  $.ajax({
    type: "GET",
    url: "test2.xml",
    //url: "test2.xml",
    crossDomain: true,
    dataType: "xml",
    success: function(xml){
    $(xml).find('book').each(function(){
      var sTitle = $(this).find('title').text();
      var author = $(this).find('author').text();
      var year = $(this).find('year').text();
      var price = $(this).find('price').text();
      $("<li></li>").html(sTitle + ", " + author + ", " + year + ", " + price ).appendTo("#list ul");
    });
  },
  error: (function(xhr, ajaxOptions, thrownError) {
    ///show error message ie. "Status Code: ("+xhr.status+")<br>Description: "+xhr.statusText
    alert("An error occurred while processing XML file. Status Code: ("+xhr.status+")<br>Description: "+xhr.statusText );
  })
  });
});
</script>

html代码:

<body>
<div id="list"></div>
</body>

输出是:

  • 日常意大利语,Giada De Laurentiis,2005,30.00
  • 哈利·波特,J·K·罗琳,2005,29.99
  • XQuery Kick Start,James McGovernPer BothnerKurt CagleJames LinnVaidyanathan Nagarajan,2003,49.99
  • 学习 XML,Erik T. Ray,2003,39.95
于 2012-07-19T05:56:23.670 回答