QueryPath PHP 中的jQuery


Posted in PHP onApril 11, 2010

官方主页  http://querypath.org/

QueryPath(QP)库 在 PHP 中实现了类似于 jQuery 的效果,用它还可以方便地处理 XML HTML...功能太强大了!!!

A QueryPath Tutorial(一个简易说明)
QueryPath makes use of method chaining to provide a concise suite of tools for manipulating a DOM.
The basic principle of method chaining is that each method returns an object upon which additional methods can be called. In our case, the QueryPath object usually returns itself.
Let's take a look at an example to illustrate:
$qp = qp(QueryPath::HTML_STUB); // Generate a new QueryPath object.(创建一个 QP 对象)
$qp2 = $qp->find('body'); // Find the body tag.(找到 "body" 标签)
// Now the surprising part:(请看下面让你惊奇的地方)
if ($qp === $qp2) {
// This will always get printed.(它总是会这样输出)
print "MATCH";
}
Why does $qp always equal $qp2? Because the find() function does all of its data gathering and then returns the QueryPath object.
This might seem esoteric, but it all has a very practical rationale. With this sort of interface, we can chain lots of methods together:
(你可以向使用 jQuery 一样来连缀方法)
qp(QueryPath::HTML_STUB)->find('body')->text('Hello World')->writeHTML();
In this example, we have four method calls:
qp(QueryPath::HTML_STUB): Create a new QueryPath object and provide it with a stub of an HTML document. This returns the QueryPath object.
find('body'): This searches the QueryPath document looking for an element named 'body'. That element is, of course, the <body></body> portion of the HTML document. When it finds the body element, it keeps an internal pointer to that element, and it returns the QueryPath object (which is now wrapping the body element).
text('Hello World'): This function takes the current element(s) wrapped by QueryPath and adds the text Hello World. As you have probably guessed, it, too, returns a QueryPath object. The object will still be pointing to the body element.
writeHTML(): The writeHTML() function prints out the entire document. This is used to send the HTML back to the client. You'll never guess what this function returns. Okay, you guessed it. QueryPath.
So at the end of the chain above, we would have created a document that looks something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 
<title>Untitled</title> 
</head> 
<body>Hello World</body> 
</html>

Most of that HTML comes from the QueryPath::HTML_STUB. All we did was add the Hello World text inside of the <body></body> tags.
Not all QueryPath functions return QueryPath objects. Some tools need to return other data. But those functions are well-documented in the included documentation.
These are the basic principles behind QueryPath. Now let's take a look at a larger example that exercises more of the QueryPath API.
A Longer Example
This example illustrates various core features of QueryPath.
In this example, we use some of the standard QueryPath functions (most of them implementing the jQuery interface) to build a new web page from scratch.
Each line of the code has been commented individually. The output from this is shown in a separate block beneath.
<?php 
/** 
* Using QueryPath. 
* 
* This file contains an example of how QueryPath can be used 
* to generate web pages. 
* @package QueryPath 
* @subpackage Examples 
* @author M Butcher <matt@aleph-null.tv> 
* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license. 
*/ 
// Require the QueryPath core. 
require_once 'QueryPath/QueryPath.php'; 
// Begin with an HTML stub document (XHTML, actually), and navigate to the title. 
qp(QueryPath::HTML_STUB, 'title') 
// Add some text to the title 
->text('Example of QueryPath.') 
// Now look for the <body> element 
->find(':root body') 
// Inside the body, add a title and paragraph. 
->append('<h1>This is a test page</h1><p>Test text</p>') 
// Now we select the paragraph we just created inside the body 
->children('p') 
// Add a 'class="some-class"' attribute to the paragraph 
->attr('class', 'some-class') 
// And add a style attribute, too, setting the background color. 
->css('background-color', '#eee') 
// Now go back to the paragraph again 
->parent() 
// Before the paragraph and the title, add an empty table. 
->prepend('<table id="my-table"></table>') 
// Now let's go to the table... 
->find('#my-table') 
// Add a couple of empty rows 
->append('<tr></tr><tr></tr>') 
// select the rows (both at once) 
->children() 
// Add a CSS class to both rows 
->addClass('table-row') 
// Now just get the first row (at position 0) 
->eq(0) 
// Add a table header in the first row 
->append('<th>This is the header</th>') 
// Now go to the next row 
->next() 
// Add some data to this row 
->append('<td>This is the data</td>') 
// Write it all out as HTML 
->writeHTML(); 
?>

The code above produces the following HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> 
<title>Example of QueryPath.</title> 
</head> 
<body> 
<table id="my-table"> 
<tr class="table-row"><th>This is the header</th></tr> 
<tr class="table-row"><td>This is the data</td></tr> 
</table> 
<h1>This is a test page</h1> 
<p class="some-class" style="background-color: #eee">Test text</p></body> 
</html>

Now you should have an idea of how QueryPath works. Grab a copy of the library and try it out! Along with the source code, you will get a nice bundle of HTML files that cover every single public function in the QueryPath library (no kidding). There are more examples there, too.
不错的东东!赶紧 Grab 它吧~~!
PHP 相关文章推荐
PHP 读取Postgresql中的数组
Apr 14 PHP
PHP CURL获取返回值的方法
May 04 PHP
php通过数组实现多条件查询实现方法(字符串分割)
May 06 PHP
PHP邮件发送类PHPMailer用法实例详解
Sep 22 PHP
typecho插件编写教程(五):核心代码
May 28 PHP
PHP读书笔记_运算符详解
Jul 01 PHP
基于CI框架的微信网页授权库示例
Nov 25 PHP
PHP实现的同步推荐操作API接口案例分析
Nov 30 PHP
Laravel5.5以下版本中如何自定义日志行为详解
Aug 01 PHP
thinkPHP5.0框架事务处理操作简单示例
Sep 07 PHP
PHP生成随机码的思路与方法实例探索
Apr 11 PHP
PHP 文件写入和读取操作实例详解【必看篇】
Nov 04 PHP
10个可以简化php开发过程的MySQL工具
Apr 11 #PHP
Fatal error: Call to undefined function curl_init()解决方法
Apr 09 #PHP
PHP Socket 编程
Apr 09 #PHP
有关JSON以及JSON在PHP中的应用
Apr 09 #PHP
dedecms系统的广告设置代码 基础版本
Apr 09 #PHP
PHP 动态随机生成验证码类代码
Apr 09 #PHP
DedeCMS 核心类TypeLink.class.php摘要笔记
Apr 07 #PHP
You might like
PHP把JPEG图片转换成Progressive JPEG的方法
2014/06/30 PHP
带你了解PHP7 性能翻倍的关键
2015/11/19 PHP
音乐播放用的的几个函数
2006/09/07 Javascript
js 学习笔记(三)
2009/12/29 Javascript
NodeJS学习笔记之网络编程
2014/08/03 NodeJs
js实现简单鼠标跟随效果的方法
2015/04/10 Javascript
Bootstrap学习系列之使用 Bootstrap Typeahead 组件实现百度下拉效果
2016/07/07 Javascript
jQuery查找节点方法完整实例
2016/09/13 Javascript
Javascript使用SWFUpload进行多文件上传
2016/11/16 Javascript
微信小程序图片横向左右滑动案例
2017/05/19 Javascript
JS实现弹出下载对话框及常见文件类型的下载
2017/07/13 Javascript
ES6学习教程之模板字符串详解
2017/10/09 Javascript
npm 下载指定版本的组件方法
2018/05/17 Javascript
PWA介绍及快速上手搭建一个PWA应用的方法
2019/01/27 Javascript
vue 中的 render 函数作用详解
2020/02/28 Javascript
Vue项目页面跳转时浏览器窗口上方显示进度条功能
2020/03/26 Javascript
Python实现的检测网站挂马程序
2014/11/30 Python
Python的函数的一些高阶特性
2015/04/27 Python
在 Python 应用中使用 MongoDB的方法
2017/01/05 Python
python中获得当前目录和上级目录的实现方法
2017/10/12 Python
python3库numpy数组属性的查看方法
2018/04/17 Python
Python Django简单实现session登录注销过程详解
2019/08/06 Python
PyQt 图解Qt Designer工具的使用方法
2019/08/06 Python
python pycharm的安装及其使用
2019/10/11 Python
在Django中预防CSRF攻击的操作
2020/03/13 Python
Python实现验证码识别
2020/06/15 Python
香港零食网购:上仓胃子
2020/06/08 全球购物
优秀技术工人先进材料
2014/02/17 职场文书
技术经济专业求职信
2014/09/03 职场文书
商场收银员岗位职责
2015/04/07 职场文书
2016年秋季新学期致辞
2015/07/30 职场文书
护理培训心得体会
2016/01/22 职场文书
廉政党课工作报告案例
2019/06/21 职场文书
遇事可以测出您的见识与格局
2019/09/16 职场文书
Python中else的三种使用场景
2021/06/16 Python
聊聊Lombok中的@Builder注解使用教程
2021/11/17 Java/Android