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 相关文章推荐
一个从别的网站抓取信息的例子(域名查询)
Oct 09 PHP
php递归列出所有文件和目录的代码
Sep 10 PHP
CI框架源码阅读,系统常量文件constants.php的配置
Feb 28 PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
Sep 30 PHP
PHP5.2下preg_replace函数的问题
May 08 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
Mar 21 PHP
详解PHP的Yii框架中自带的前端资源包的使用
Mar 31 PHP
php中让人头疼的浮点数运算分析
Oct 10 PHP
记录一次排查PHP脚本执行卡住的问题
Dec 27 PHP
Laravel中日期时间处理包Carbon的简单使用
Sep 21 PHP
PHP时间日期增减操作示例【date strtotime实现加一天、加一月等操作】
Dec 21 PHP
php字符串截取函数mb_substr用法实例分析
Jun 25 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 ob_flush,flush在ie中缓冲无效的解决方法
2010/05/09 PHP
浅析PHP中的字符串编码转换(自动识别原编码)
2013/07/02 PHP
给ECShop添加最新评论
2015/01/07 PHP
对laravel in 查询的使用方法详解
2019/10/09 PHP
Laravel框架处理用户的请求操作详解
2019/12/20 PHP
通过javascript设置css属性的代码
2009/12/28 Javascript
JavaScript中yield实用简洁实现方式
2010/06/12 Javascript
ASP.NET中基于JQUERY的高性能的TreeView补充
2011/02/23 Javascript
JavaScript 命名空间 使用介绍
2013/08/29 Javascript
用JS将搜索的关键字高亮显示实现代码
2013/11/08 Javascript
js获取通过ajax返回的map型的JSONArray的方法
2014/01/09 Javascript
原生Ajax 和jQuery Ajax的区别示例分析
2014/12/17 Javascript
js+html5获取用户地理位置信息并在Google地图上显示的方法
2015/06/05 Javascript
javascript事件委托的方式绑定详解
2015/06/10 Javascript
关于动态生成dom绑定事件失效的原因及解决方法
2016/08/06 Javascript
关于vue-resource报错450的解决方案
2017/07/24 Javascript
JS基于递归实现网页版计算器的方法分析
2017/12/20 Javascript
js闭包学习心得总结
2018/04/17 Javascript
jQuery实现的简单拖拽功能示例【测试可用】
2018/08/14 jQuery
深入理解Angularjs 脏值检测
2018/10/12 Javascript
python中根据字符串调用函数的实现方法
2016/06/12 Python
python使用turtle库与random库绘制雪花
2018/06/22 Python
Python中new方法的详解
2019/01/15 Python
Django如何将URL映射到视图
2019/07/29 Python
python对Excel的读取的示例代码
2020/02/14 Python
python3 实现口罩抽签的功能
2020/03/11 Python
详解如何使用Pytest进行自动化测试
2021/01/14 Python
python如何发送带有附件、正文为HTML的邮件
2021/02/27 Python
医学生自我鉴定范文
2013/11/08 职场文书
采购主管工作职责
2013/12/12 职场文书
应届毕业生个人求职信范文
2014/01/29 职场文书
导游词格式
2015/02/13 职场文书
2015国庆节66周年演讲稿
2015/03/20 职场文书
质量承诺书格式范文
2015/04/28 职场文书
在校大学生才艺比赛策划书怎么写?
2019/08/26 职场文书
Java基础之详解HashSet的使用方法
2021/06/30 Java/Android