Xpath可以对XML文件中的信息进行查找,并对XML文件树形结构进行遍历。详细语法请转:http://www.w3school.com.cn/xpath/index.asp

例子:利用在JavaScript脚本中利用Xpath对XML文件中内容进行查询,将查询结果显示到html文件中

ex1.html文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Xpath Basis-1</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Wayne Ng" />
<meta name="description" content="Xpath basis1" />
<meta name="revised" content="Wayne Ng, 2016/1/26" />
</head>
<body onload = "startLoad()">
<p>所有英雄职业:</p>
<div id = "text"></div>
<script type="text/javascript" src="func1.js"></script>
</body>
</html>

ex1.xml文件:

<?xml version = "1.0" encoding = "UTF-8"?>
<!--
Date:2016/1/26
Writer:Wayne Ng
Theme:MapleStory hero briefInstruction
-->
<game>
<warrior>
<hero>
<name>英雄</name>
<weapons>剑、斧、钝器</weapons>
<skill>终极打击</skill>
</hero>
<hero>
<name>圣骑士</name>
<weapons>剑、钝器</weapons>
<skill>神圣冲击</skill>
</hero>
<hero>
<name>黑骑士</name>
<weapons>长枪、矛</weapons>
<skill>黑暗穿刺</skill>
</hero>
</warrior>
<magician>
<hero>
<name>主教</name>
<weapons>长杖、短仗</weapons>
<skill>圣光普照</skill>
</hero>
<hero>
<name>火毒法师</name>
<weapons>长杖、短仗</weapons>
<skill>末日火焰</skill>
</hero>
<hero>
<name>冰雷法师</name>
<weapons>长杖、短仗</weapons>
<skill>冰咆哮</skill>
</hero>
</magician>
<archer>
<hero>
<name>神射手</name>
<weapons>弓</weapons>
<skill>暴风箭雨</skill>
</hero>
<hero>
<name>箭神</name>
<weapons>弩</weapons>
<skill>终极扫射</skill>
</hero>
</archer>
<ranger>
<hero>
<name>侠盗</name>
<weapons>短剑、短刀</weapons>
<skill>暗杀</skill>
</hero>
<hero>
<name>隐士</name>
<weapons>拳套</weapons>
<skill>四连镖</skill>
</hero>
<hero>
<name>暗影双刀</name>
<weapons>短剑、短刀</weapons>
<skill>终极斩</skill>
</hero>
</ranger>
</game>

func1.js文件:

//载入待解析XML文件
function loadXMLDoc(name){
if(window.XMLHttpRequest){
var xhttp = new XMLHttpRequest();
}
else{
var xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", name , false);
xhttp.send(null);
return xhttp.responseXML;
} function startLoad(){
var xml = loadXMLDoc("ex1.xml");
var path = "//name";
var txt = "";
//利用Xpath来查找感兴趣消息
if(window.ActiveXObject){
var nodes = xml.selectNodes(path);
for(var i = 0; i < nodes.length; ++i){
txt += nodes[i].childNodes[0].nodeValue + "<br />";
}
}
else{
var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
while(result){
txt += result.childNodes[0].nodeValue + "<br />";
result = nodes.iterateNext();
}
}
document.getElementById("text").innerHTML = txt;
}

显示效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ0AAAE1CAIAAAByWSKfAAAOw0lEQVR4nO3d23WjMBSFYeqiIOqhGpqhGM8DCHQ5R1s4OGOR/3uZxMaXWUs7QiAdDS8AyvC/vwDQAXICaOQE0MgJoJETQCMngEZOAI2cABo5ATRyAmjkBNDICaCRE0AjJ4BGTgCNnACazskyDcMwTEv66DqPg/V4eG6c15aPX+cxfYvKa/Njl2kYpmWdx8bParRM5xcovh7+KCcnWziGYRjORhM95gTkPK61cV3JSdyA44/JPy9EeEi/bvr1s/+b8zHkBBs/J0Ubsh4LT7SbFrMde85PLGMSfl3nMUtm9GQcp+Tre4kkJyjdkZNI2WT18ao/KXM1zouXte21F3OS9EhuTi6cTuJp9HnX3gu05GSdR/tcRpNvnhywTFFXY/zRz76+7E/in72cLNPxkT/4f6JPDf1J+GurmrI6/UpfrE/WvDOp0MHUc3LtvCvtXKycxH0OOflzruXEb/21618v65zFH26E47NrW+O8bv8s8zjOsx2zsq03jk/OE0Y7J1c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x8RJql54yzsr4F5t8yDl9a7yDGy/zV4EwLzxbjfy/QknBGkfRmeCT2rPibredbTb6ECqE+EZWKcFaOQE0MgJoJETQCMngEZOAI2cABo5ATRyAmjkBNDICaCRE0AjJ4BGTgCNnAAaOQE0cgJo5ATQyAmgkRNAIyeARk4AjZwAGjkBNHICaOQE0MgJoJETQCMngEZOAO0feHdVznKCnA8AAAAASUVORK5CYII=" alt="" />

      2016/1/26  By 野马菌

自学Xpath的几个例子的更多相关文章

  1. 自学HTML的几个例子

    此处不赘述HTML中不同标签的用法仅仅给出自己学习时写的一些自娱自乐的例子,具体标签用法请参考W3C(http://www.w3school.com.cn/),毕竟这个才是最靠谱的,请不要相信任何二道 ...

  2. 自学php的几个例子(包含AMP(Apache、MySQL、PHP)环境搭建链接)

    学习PHP之前需要先搭建PHP运行的环境(即服务器+PHP+数据库)来使PHP成功运行,具体环境搭建教程可参考pharen(http://www.cnblogs.com/pharen/archive/ ...

  3. 自学XSL的几个例子

    XSL 指扩展样式表语言(EXtensible Stylesheet Language).XSL用来描述XML文件的格式,类似于我们可以用CSS描述HTML的格式.具体用法请转:http://www. ...

  4. 自学xml的几个例子

    xml是一种被用来传输和存储数据的语言,下面给出一些学习xml过程一些简单的例子.具体xml语法请转:http://www.w3school.com.cn/xml/xml_intro.asp 例子1: ...

  5. 自学JavaScript的几个例子

    学习了广泛使用的浏览器脚本JavaScript和HTML的DOM模型(也是用JavaScript实现),下面给出两个自己学习时的例子,具体JavaScript语法请参考W3C相关网页(http://w ...

  6. Xpath定位元素-一个例子

    前几天在群里面解决的问题,记录下来和大家分享 需要定位这个股份制企业 方法: # 首先需要单击下拉框弹出企业性质的下拉选项:然后用过Xpath定位元素 driver.find.element_by_c ...

  7. 自学Zabbix3.3-一个简单例子 添加Hosts并应用模板

    Host 是 Zabbix 监控的基本载体,所有的监控项都是基于 host 的. 通过 Configuration->Hosts->Create Host 来创建监控设备 按提示填入 Na ...

  8. 自学Zabbix之路

    自学Zabbix之路[第一回]:初识Zabbix 1.1 自学Zabbix1.1-简介 1.2 自学Zabbix1.2-zabbix特性 1.3 自学Zabbix1.3-zabbix进程 自学Zabb ...

  9. XPath和CssSelector定位总结

    1. 介绍XPath和CssSelector 2. XPath有哪些方式 2.1 通过XPath语法 2.2 Contains关键字 2.3 Start-With 2.4 Or和And关键字 2.5 ...

随机推荐

  1. poj 1940 Wine Trading in Gergovia_贪心

    在一条街上有许多房屋,每间屋子里都住着人,并且都是做葡萄酒生意的商人,他们每天都要决定买卖多少瓶葡萄酒.有趣的地方是,供需总是完美地一致.商人总是能买到自己需要的葡萄酒,并且,他们从来不介意是从哪个商 ...

  2. Linux(gnu)环境动态链接库的搜索路径

    摘自http://blog.csdn.net/saga1979/article/details/14161229 man ld.so(8)说,如果库依赖不包括“/”,那么它将按照下面的规则按顺序搜索: ...

  3. LINUX 网络编程 原始套接字

    一 原始套接字 原始套接字(SOCK_RAW)是一种不同于SOCK_STREAM.SOCK_DGRAM的套接字,它实现于系统核心.然而,原始套接字能做什么呢?首先来说,普通的套接字无法处理ICMP.I ...

  4. Trie树-脏词过滤应用

    Trie树,又称字符查找树.前缀树,主要用于字符匹配(详见http://en.wikipedia.org/wiki/Trie).适合做关键词查找,比如查找文章中的关键字然后给他们加链接. 当然对脏词的 ...

  5. Oracle SecureFiles 说明(转)

    Oracle SecureFiles 说明 Oracle Database 11g 将LOB 数据类型作为Oracle SecureFiles 进行了完全重新设计,显著改进了应用程序开发的性能.可管理 ...

  6. Javascript 常用代码总结

    1. document.referrer可以获得上一页的地址,使用document.anchors获得页面上面所有的链接元素,而不必使用 document.getElementsByTagName(' ...

  7. 使用ADO.net中的链接字符串

    需要引用:System.Configuration命名空间 ConfigurationManager.ConnectionStrings["sqlConnStr"].Connect ...

  8. MVC 传参

    介绍一些View中常用的东西 1:传递参数: 1):路由协议中传递参数: 1):eg:比如是这样类似的路由协议,那么我们在传递参数的时候,就要传递 id过去,当然如果,ABCD= UrlParamet ...

  9. OpenGL鼠标旋转图像

    (鼠标旋转功能) #include <iostream> using namespace std; #include<gl/glut.h> GLfloat transx,tra ...

  10. 贝叶斯网络基础(Probabilistic Graphical Models)

    本篇博客是Daphne Koller课程Probabilistic Graphical Models(PGM)的学习笔记. 概率图模型是一类用图形模式表达基于概率相关关系的模型的总称.概率图模型共分为 ...