PHP文档的类似jQuery的选择器
我正在使用一个,我想知道是否存在一些使用类似CSS的选择器来选择节点的方法,就像我们在中所做的那样 示例情况:我正在解析一个XML文件,其中一个片段如下所示:PHP文档的类似jQuery的选择器,php,jquery,xml,dom,css-selectors,Php,Jquery,Xml,Dom,Css Selectors,我正在使用一个,我想知道是否存在一些使用类似CSS的选择器来选择节点的方法,就像我们在中所做的那样 示例情况:我正在解析一个XML文件,其中一个片段如下所示: <gesmes:Envelope> <gesmes:subject>Reference rates</gesmes:subject> <gesmes:Sender> <gesmes:name>European Central Bank</ge
<gesmes:Envelope>
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2009-07-13">
<Cube currency="USD" rate="1.3975"/>
<Cube currency="JPY" rate="129.03"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="26.028"/>
</Cube>
</Cube>
</gesmes:Envelope>
$xpath = new DOMXpath($myDomDocument);
$cubesWithCurrencies = $xpath->query('//cube[@currency]');
检索具有“currency”属性的所有Cube
元素
但是,如何使用PHP的
DOMDocument
?我想选择具有属性或具有特定属性值的元素。XPath正是您要查找的内容,看看PHP中的类。它使用XPath,所以如果您不熟悉XPath语法,就需要阅读XPath语法。上面有一些文档,如果你特别勇敢的话,也可以阅读
要解决示例问题://cube[@currency]
是一个XPath查询,用于选择文档中具有currency属性的所有元素。将其与DOMXPath
类一起使用时如下所示:
<gesmes:Envelope>
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2009-07-13">
<Cube currency="USD" rate="1.3975"/>
<Cube currency="JPY" rate="129.03"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="26.028"/>
</Cube>
</Cube>
</gesmes:Envelope>
$xpath = new DOMXpath($myDomDocument);
$cubesWithCurrencies = $xpath->query('//cube[@currency]');
$cubeswithcurrences
现在是一个可以迭代的工具。如果您想操作domalajquery,PHPQuery是您的选择
一个简单的例子,你可以用它做什么
// almost everything can be a chain
$li = null;
$doc['ul > li']
->addClass('my-new-class')
->filter(':last')
->addClass('last-li');
您可以使用Symfony DomCrawler组件,使您能够使用css选择器进行DOM遍历:
我创建了一个库,允许您像使用jQuery一样抓取HTML5和XML文档 你可以找到图书馆 它应该允许你做你想做的事情 在引擎盖下,它用于将CSS选择器转换为XPath选择器。它总是使用相同的DomDocument,即使在将一个对象传递给另一个对象时也是如此,以确保良好的性能 该库还包括自己的用于PSR-0兼容库的零配置自动加载程序。所包含的示例应该是开箱即用的,不需要任何额外的配置
示例用法:
namespace PowerTools;
// Get file content
$htmlcode = file_get_contents( 'https://github.com' );
// Define your DOMCrawler based on file string
$H = new DOM_Query( $htmlcode );
// Define your DOMCrawler based on an existing DOM_Query instance
$H = new DOM_Query( $H->select('body') );
// Passing a string (CSS selector)
$s = $H->select( 'div.foo' );
// Passing an element object (DOM Element)
$s = $H->select( $documentBody );
// Passing a DOM Query object
$s = $H->select( $H->select('p + p') );
// Select the body tag
$body = $H->select('body');
// Combine different classes as one selector to get all site blocks
$siteblocks = $body->select('.site-header, .masthead, .site-body, .site-footer');
// Nest your methods just like you would with jQuery
$siteblocks->select('button')->add('span')->addClass('icon icon-printer');
// Use a lambda function to set the text of all site blocks
$siteblocks->text(function( $i, $val) {
return $i . " - " . $val->attr('class');
});
// Append the following HTML to all site blocks
$siteblocks->append('<div class="site-center"></div>');
// Use a descendant selector to select the site's footer
$sitefooter = $body->select('.site-footer > .site-center');
// Set some attributes for the site's footer
$sitefooter->attr(array('id' => 'aweeesome', 'data-val' => 'see'));
// Use a lambda function to set the attributes of all site blocks
$siteblocks->attr('data-val', function( $i, $val) {
return $i . " - " . $val->attr('class') . " - photo by Kelly Clark";
});
// Select the parent of the site's footer
$sitefooterparent = $sitefooter->parent();
// Remove the class of all i-tags within the site's footer's parent
$sitefooterparent->select('i')->removeAttr('class');
// Wrap the site's footer within two nex selectors
$sitefooter->wrap('<section><div class="footer-wrapper"></div></section>');
[...]
名称空间PowerTools;
//获取文件内容
$htmlcode=文件\u获取\u内容('https://github.com' );
//根据文件字符串定义您的DOMCrawler
$H=新的DOM\U查询($htmlcode);
//基于现有的DOM_查询实例定义您的DOMCrawler
$H=新的DOM_查询($H->select('body');
//传递字符串(CSS选择器)
$s=$H->select('div.foo');
//传递元素对象(DOM元素)
$s=$H->select($documentBody);
//传递DOM查询对象
$s=$H->select($H->select('p+p'));
//选择主体标记
$body=$H->select('body');
//将不同的类组合为一个选择器,以获取所有站点块
$siteblocks=$body->select('.site header、.mashead、.site body、.site footer');
//像使用jQuery一样嵌套方法
$siteblocks->select('button')->add('span')->addClass('icon-icon-printer');
//使用lambda函数设置所有站点块的文本
$siteblocks->text(函数($i,$val){
返回$i。“-”$val->attr('class');
});
//将以下HTML附加到所有站点块
$siteblocks->append(“”);
//使用子体选择器选择站点的页脚
$sitefooter=$body->select('.site footer>.site center');
//设置网站页脚的一些属性
$sitefooter->attr(数组('id'=>'aweesome','data val'=>'see');
//使用lambda函数设置所有场地块的属性
$siteblocks->attr('data-val',函数($i,$val){
返回$i。“-”$val->attr('class')。“-凯利·克拉克摄”;
});
//选择网站页脚的父项
$sitefooterparent=$sitefooter->parent();
//删除站点页脚父级中所有i标记的类
$sitefooterparent->select('i')->removeAttr('class');
//将站点的页脚包装在两个nex选择器中
$sitefooter->wrap(“”);
[...]
支持的方法:
- [x] (一)
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x] (二)
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
- [x]
哇,太棒了!我唯一的疑问是未来的可靠性。。。如果项目将停止怎么办?我想使用DomDocument会更安全。。但我要试一试!它向后使用DOMDocument,因此您不必担心;)最后,它只是一个包装器,可以为您的所有DOM需求提供语法糖……是的,我看到它实际上只是PHPDOM的API的一个API;)但它的工作原理很神奇,即使是大文件。谢谢你,伙计-1.phpQuery很差劲,在经历了可怕的缺陷之后,我建议永远不要使用它。它是一个废弃软件(上一次提交于2010年2月),包含了一些非常基本的东西,比如错误。远离这一点;我是一个phpquery的忠实粉丝,能够使用javascript中使用的相同css3查询,真是太棒了。除了原生DOMDocument的字符编码问题外,我从来没有遇到过phpquery的问题,而且看起来一年前有过提交。他们的文档确实有点糟糕,虽然XPath适合按元素名选择,但按属性值(类、id等)选择却很糟糕。此外,它会阻塞无效的XML,并且web上的大多数HTML都不是有效的XML。实际上,您可以使用DOMDocument::loadHTML()加载不正确的HTML,并使用XPath查询。@LucasBustamante:通常,您应该能够下载repo并在“examples”文件夹中运行示例,而无需任何其他配置。这对你有用吗?如果没有,会发生什么?如果是,您将如何尝试使用该库?同时安装CssSelector组件并使用filter()!