给出下面的XML:
<div class="fly">
<img src="a.png" class="badge">
<img class="aye" data-original="b.png" width="130" height="253" />
<div class="to">
<h4>Fly To The Moon</h4>
<div class="clearfix">
<div class="the">
<h4>**Wow**</h4>
</div>
<div class="moon">
<h4>**Great**</h4>
</div>
</div>
</div>
</div>
你问:
我怎样才能从<img class="aye">
得到data-original
值,也从<h4>
标签获得的价值 “哇” 和 “大”?
在XPath,你可以得到的值作为字符串直接:
string(//div[@class='fly']/img/@data-original)
这是一个img标签的第一个数据,原始属性带class =“飞翔”的所有div内的字符串。
string(//div[@class='fly']//h4[not(following-sibling::*//h4)][1])
string(//div[@class='fly']//h4[not(following-sibling::*//h4)][2])
这些都是后面没有对所有div class="fly"
在它自己的级别由另一<h4>
标签第一和第二<h4>
标签的字符串值。
这看起来有点像现在的方式站立,但随着迭代,在前面的部分将不再需要任何再快,因为XPath的话会相对的:
//div[@class='fly']
string(./img/@data-original)
string(.//h4[not(following-sibling::*//h4)][1])
string(.//h4[not(following-sibling::*//h4)][2])
使用XPath string(...)
表达式在PHP中,您必须使用DOMXPath::evaluate()
而不是DOMXPath::query()
。那么这将如下所示:
$aye = $xpath->evaluate("string(//div[@class='fly']/img/@data-original)");
$h4_1 = $xpath->evaluate("string(//div[@class='fly']//h4[not(following-sibling::*//h4)][1])");
$h4_2 = $xpath->evaluate("string(//div[@class='fly']//h4[not(following-sibling::*//h4)][2])");
与迭代和输出的完整的例子:
// all <div> tags with class="fly"
$divs = $xpath->evaluate("//div[@class='fly']");
foreach ($divs as $div) {
// the first data-original attribute of an <img> inside $div
echo $xpath->evaluate("string(./img/@data-original)", $div), "<br/>\n";
// all <h4> tags anywhere inside the $div
$h4s = $xpath->evaluate('.//h4[not(following-sibling::*//h4)]', $div);
foreach ($h4s as $h4) {
echo $h4->nodeValue, "<br/>\n";
}
}
如示例所示,您可以使用评估以及对节点列表,太。从所有的<h4>
获得的值不再与string()
标记,因为我认为可能不止两个。
Online Demo including special string output (just exemplary):
echo <<<HTML
{$xpath->evaluate("string(//div[@class='fly']/img/@data-original)")}<br/>
{$xpath->evaluate("string(//div[@class='fly']//h4[not(following-sibling::*//h4)][1])")}<br/>
{$xpath->evaluate("string(//div[@class='fly']//h4[not(following-sibling::*//h4)][2])")}<br/>
<hr/>
HTML;
鬼,你知道如何从jQuery的AJAX解析数据恢复?它隐藏在html标签中。谢谢。 – 2014-10-10 11:43:28
@ AndrewB.O。你什么意思不起作用?你看过演示了吗?并且关于那个隐藏的输入,没有一个ajax渲染的动态值不能用这种方式解析,你需要使用phantomjs或者selenium来做这个 – Ghost 2014-10-10 11:48:43
是的。我已经看过演示,但是当我将你的代码应用于我的案例(通过布局进行说明)时,会给出不同的结果。所以,我改变了一下你的代码。很多人都很感谢你的帮助:) PhantomJS在客户端运行,我在服务器端解析文档。我如何使用它? – 2014-10-11 06:44:16