使用simplexml加载解析xml

时间:2014-07-07 01:04:20

标签: php xml simplexml

我正在尝试解析xml,但在尝试获取图片网址时遇到问题。

我的xml是:

<entry>
<title>The Title</title>
<id>http://example.com/post/367327.html</id>
<summary>Some extra text</summary>
<link rel="enclosure" href="http://example.com/photos/f_0px_30px/image687.jpg" type="image/jpeg"  length="" /> 
</entry>

到目前为止,我使用下面的代码来获取其他数据:

$url = "http://msdssite.com/feeds/xml/myxml.xml";
$xml = simplexml_load_file($url);

foreach($xml->entry as $PRODUCT)
{
$my_title = trim($PRODUCT->title);
$url = trim($PRODUCT->id);
$myimg = $PRODUCT->link;
}

如何从此解析href<link rel="enclosure" href="http://example.com/photos/f_0px_30px/image687.jpg" type="image/jpeg" length="" />

3 个答案:

答案 0 :(得分:2)

由于您的条目似乎可以包含多个link标记,因此您需要检查type属性是否具有值image/jpeg,以确保获取图像的链接:

ini_set("display_errors", "On");

$feedURL = 'http://OLDpost.gr/feeds/xml/category-takhs-xatzhs.xml';

$feed = simplexml_load_file($feedURL);

$results = array();

foreach($feed->entry as $entry) {
    $result = array('title' => (string)$entry->title,
                    'url'   => (string)$entry->id);

    $links = $entry->link;
    foreach ($links as $link) {
        $linkAttr = $link->attributes();
        if (isset($linkAttr['type']) && $linkAttr['type']=='image/jpeg') {
            $result['img'] = (string)$linkAttr['href'];
            break;
        }
    }
    $results[] = $result;
}

print_r($results);

请注意,使用像这样的simplexml(foreach循环来查找好的链接标记)并不是很方便。使用XPath查询更好:

foreach($feed->entry as $entry) {
   $entry->registerXPathNamespace('e', 'http://www.w3.org/2005/Atom');
   $results[] = array(
     'title' => (string)$entry->title,
     'url'   => (string)$entry->id,
     'img'   => (string)$entry->xpath('e:link[@type="image/jpeg"]/@href')[0][0]
   );
}

答案 1 :(得分:1)

如果这是确切的XML,实际上不需要foreach。试试这个:

$xml = simplexml_load_file($url);

$my_title = (string) $xml->title;
$myimg = (string) $xml->link->attributes()['href']; // 5.4 or above
echo $myimg; // http://example.com/photos/f_0px_30px/image687.jpg

答案 2 :(得分:0)

尝试:

foreach($xml->entry as $PRODUCT)
{
$my_title = trim($PRODUCT->title[0]);
$url = trim($PRODUCT->id[0]);
$myimg = $PRODUCT->link[0];
}