当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery实现的解析本地 XML 文档操作示例

jQuery实现的解析本地 XML 文档操作示例

2020年05月10日  | 移动技术网IT编程  | 我要评论
本文实例讲述了jquery实现的解析本地 xml 文档操作。分享给大家供大家参考,具体如下:create a jquery object using an xml string and obtain

本文实例讲述了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>

ps:这里再为大家提供几款关于xml操作相关在线工具供大家参考使用:

在线xml/json互相转换工具:

在线格式化xml/在线压缩xml

xml在线压缩/格式化工具:

xml代码在线格式化美化工具:

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网