【发布时间】:2014-09-03 13:25:03
【问题描述】:
我正在尝试检索包含在 SimpleXMLElement 中的一些元数据。我正在使用 XPATH,但我很难获得我感兴趣的价值。
这是网页标题的摘录(来自:http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html)
你知道我如何检索包含以下内容的数组中的所有 xmlns 数据:
1) og:类型 2)OG:网址 3) OG:图像 …… x) og:upc
<meta xmlns:og="http://opengraphprotocol.org/schema/" property="og:title" content="CleverFurn Couchtisch "Abby"" />
这是我的 php 代码
<?php
$html = file_get_contents("http://www.wayfair.de/CleverFurn-Couchtisch-Abby-69318X2-MFE2223.html");
$doc = new DOMDocument();
$doc->strictErrorChecking = false;
$doc->recover=true;
@$doc->loadHTML("<html><body>".$html."</body></html>");
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*/meta[@property='og:url']");
if (!is_null($elements)) {
foreach ($elements as $element) {
echo "<br/>[". $element->nodeName. "]";
var_dump($element);
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
【问题讨论】:
标签: php html xml xpath simplexml