Javascript-XPath

JavaScript-XPath是一个基于Javascript的高效的DOM 3 XPath (XPath 1.0)的解析器,支持IE6以上版本以及所有其它的现代浏览器

官方网站为:http://coderepos.org/share/wiki/JavaScript-XPath

官网示例:

 
<html>
 <head><title>Hello, DOM 3 XPath!</title>
 
  <!-- import JavaScript-XPath -->
  <script type="text/javascript" src="javascript-xpath.js"></script>
 
  <!-- use XPath -->
  <script type="text/javascript">
// <![CDATA[
function main() {
 
  // -- First case --
  // Evaluate a XPath expression string
  var result = document.evaluate('//h1', document, null, 7, null);
  // Display the result
  var h1 = result.snapshotItem(0);
  alert(h1.innerHTML); // Hello, DOM 3 XPath!
 
  // -- Second case --
  // Create a XPath expression object
  var expr = document.createExpression('//h1', null);
  // Evaluate a XPath expression object
  result = expr.evaluate(document, 7, null);
  // Display the result
  h1 = result.snapshotItem(0);
  alert(h1.innerHTML); // Hello, DOM 3 XPath!
};
// ]]>
  </script>
 
 </head><body onload="main()"><h1>Hello, DOM 3 XPath!</h1></body>
</html>

SimpleXML真是个好东西

这两天写一个页面需要访问一个RSS并且读取rss内的的文章标题和文章的链接,就尝试了一下PHP5的新特性也就是这个SimpleXML,直接支持xpath获取,确实很方便啊 :

$file = file_get_contents("__YOUR__RSS__URL__HERE__");
 
$xml = new SimpleXMLElement($file);
 
$titles = $xml -> xpath("/rss/channel/item/title");
$links = $xml -> xpath("/rss/channel/item/link");

然后就对$titles和$links这两个数组进行操作即可。