zoukankan      html  css  js  c++  java
  • [javascript]—jQuery解析本地 XML 文档

    Create a jQuery object using an XML string and obtain the value of the title node.

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery.parseXML demo</title>
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>
     
    <p id="someElement"></p>
    <p id="anotherElement"></p>
     
    <script>
    var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
      xmlDoc = $.parseXML( xml ),
      $xml = $( xmlDoc ),
      $title = $xml.find( "title" );
     
    // Append "RSS Title" to #someElement
    $( "#someElement" ).append( $title.text() );
     
    // Change the title to "XML Title"
    $title.text( "XML Title" );
     
    // Append "XML Title" to #anotherElement
    $( "#anotherElement" ).append( $title.text() );
    </script>
     
    </body>
    </html>
    

    方法二:

    /**
     * @param	{String}	xmlFileAddr		文件地址
     */
    function parseXML(xmlFileAddr) {
            $.ajax({
                type: "GET",
                url: xmlFileAddr,
                dataType: "xml",
                success: function(data, textStatus, jqXHR){//读取成功
                    console.log(data)
                    // todo......
                },
                error: function(jqXHR, textStatus, errorThrown) {//读取失败时
                    $.alert('解析文件失败!')
                }
            });
        }
    

    使用方法:

    <script>
    
        window.onload = function() {
            parseXML("./xx/xx.xml");    //文件地址
        }
    </script>
    
  • 相关阅读:
    75分以下是文盲
    罗永浩最近怎么了
    北京奥运会赛事项目竞赛日程表
    从今天开始我的blog增加计数器
    07工作总结
    五道脑筋测试题,全答对的是天才
    转:国家名字的含义
    我的新装备双狂
    上海海鲜自助:喜多屋vs古象大酒店
    因为有了爱
  • 原文地址:https://www.cnblogs.com/fayin/p/6441573.html
Copyright © 2011-2022 走看看