设为首页 收藏本站
查看: 464|回复: 0

[经验分享] QueryPath

[复制链接]

尚未签到

发表于 2015-8-26 14:02:57 | 显示全部楼层 |阅读模式
  官方主页  http://querypath.org/


QP API 手册  http://api.querypath.org/docs/

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:




DSC0000.gif DSC0001.gif 代码



<!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 它吧~~!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-104528-1-1.html 上篇帖子: PHP.ini配置文件[中文] 下篇帖子: php判断操作系统版本及浏览器版本的函数-超级好用哦!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表