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中删除变量时unset()和null的区别分析
Jan 27 PHP
php生成zip压缩文件的方法详解
Jun 09 PHP
hadoop常见错误以及处理方法详解
Jun 19 PHP
Codeigniter框架实现获取分页数据和总条数的方法
Dec 05 PHP
php通过array_merge()函数合并关联和非关联数组的方法
Mar 18 PHP
typecho插件编写教程(五):核心代码
May 28 PHP
PHP执行linux命令常用函数汇总
Feb 02 PHP
php中使用GD库做验证码
Mar 31 PHP
PHP 常用时间函数资料整理
Oct 22 PHP
php生成毫秒时间戳的实例讲解
Sep 22 PHP
thinkphp5 加载静态资源路径与常量的方法
Dec 24 PHP
Mac M1安装mnmp (Mac+Nginx+MySQL+PHP) 开发环境
Mar 29 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中可以自动分割查询字符的Parse_str函数使用示例
2014/07/25 PHP
PHP查询大量数据内存耗尽问题的解决方法
2016/10/28 PHP
解决laravel 表单提交-POST 异常的问题
2019/10/15 PHP
alixixi runcode.asp的代码不错的应用
2007/08/08 Javascript
JavaScript—window对象使用示例
2013/12/09 Javascript
javascript表单验证使用示例(javascript验证邮箱)
2014/01/07 Javascript
jQuery取id有.的值的方法
2014/05/21 Javascript
jQuery中dequeue()方法用法实例
2014/12/29 Javascript
浅谈重写window对象的方法
2014/12/29 Javascript
jquery验证邮箱格式是否正确实例讲解
2015/11/16 Javascript
深入浅析AngularJS中的module(模块)
2016/01/04 Javascript
jQuery事件委托之Safari
2016/07/05 Javascript
JS获取和修改元素样式的实例代码
2016/08/06 Javascript
jquery延迟对象解析
2016/10/26 Javascript
Vue中添加过渡效果的方法
2017/03/16 Javascript
Angular2利用组件与指令实现图片轮播组件
2017/03/27 Javascript
jQuery简单判断值是否存在于数组中的方法示例
2018/04/17 jQuery
javascript设计模式 ? 模板方法模式原理与用法实例分析
2020/04/23 Javascript
Vue切换div显示隐藏,多选,单选代码解析
2020/07/14 Javascript
Django 日志配置按日期滚动的方法
2019/01/31 Python
Python实现最大子序和的方法示例
2019/07/05 Python
Virtualenv 搭建 Py项目运行环境的教程详解
2020/06/22 Python
Python 执行矩阵与线性代数运算
2020/08/01 Python
python删除文件、清空目录的实现方法
2020/09/23 Python
Timberland法国官网:购买靴子、鞋子、衣服、夹克和配饰
2019/11/30 全球购物
接口可以包含哪些成员
2012/09/30 面试题
演讲稿怎么写
2014/01/07 职场文书
十月份红领巾广播稿
2014/01/22 职场文书
执行总经理岗位职责
2014/02/03 职场文书
组织鉴定材料
2014/06/02 职场文书
工地安全生产标语
2014/06/06 职场文书
十佳好少年事迹材料
2014/08/21 职场文书
党员查摆剖析材料
2014/10/10 职场文书
2014小学教师年度考核工作总结
2014/12/03 职场文书
2015年手术室工作总结
2015/05/11 职场文书
2016婚礼主持词开场白
2015/11/24 职场文书