Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中使用GetElementsByTagnames检索属性?_Php_Xml - Fatal编程技术网

如何在php中使用GetElementsByTagnames检索属性?

如何在php中使用GetElementsByTagnames检索属性?,php,xml,Php,Xml,我想从“”中提取我正在创建的网站的新闻源。除了能够为每篇文章提取图像的URL位置之外,我的一切都正常工作 在此提要中,图像URL在代码中显示如下: <media:content url="http://rapfix.mtv.com/wp-content/uploads/2011/05/tyler-handcuff.jpg" type="image/jpeg" height="300" width="575"> <media:text type="plain"><![

我想从“”中提取我正在创建的网站的新闻源。除了能够为每篇文章提取图像的URL位置之外,我的一切都正常工作

在此提要中,图像URL在代码中显示如下:

<media:content url="http://rapfix.mtv.com/wp-content/uploads/2011/05/tyler-handcuff.jpg" type="image/jpeg" height="300" width="575">
<media:text type="plain"><![CDATA[tyler-handcuff]]></media:text>
</media:content>
但是现在,我正在尝试从中获取“URL”属性。下面是我的代码:

$xml=("http://rapfix.mtv.com/feed");

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

$x = $xmlDoc->getElementsByTagName('item');

foreach($x as $article){
        $item_title = $article->getElementsByTagName('title')->item(0)->nodeValue;
    $item_link = $article->getElementsByTagName('link')->item(0)->nodeValue;
    $item_desc = $article->getElementsByTagName('description')->item(0)->nodeValue;
    $item_pic = $article->getElementsByTagNameNS('http://purl.org/rss/1.0/modules/content/', 'content')->item(0);

        echo ("<strong><a href='".$item_link."' target='_blank'>".$item_title."</a></strong><br />");
    echo ("<div><div class='FloatLeft'><img src='".$item_pic."' width='100' height='100'/></div><div class='FloatLeft'>".$item_desc." - <a href='".$item_link."' target='_blank'>Read More</a></div>^");
}
$xml=(“http://rapfix.mtv.com/feed");
$xmlDoc=新的DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
foreach($x作为$article){
$item_title=$article->getElementsByTagName('title')->item(0)->nodeValue;
$item_link=$article->getElementsByTagName('link')->item(0)->nodeValue;
$item_desc=$article->getElementsByTagName('description')->item(0)->nodeValue;
$item_pic=$article->getelementsbytagnames('http://purl.org/rss/1.0/modules/content/“,”内容“)->项(0);
回声(“
”); 回音(“$item_desc.-^”); }

关于如何做到这一点有什么想法吗?

目标元素的名称空间是media。元素名为content。媒体命名空间的命名空间URL为。因此:


啊,好吧,我搞混了我应该用哪一个名称空间。现在,我继续进行了必要的更改,但是我得到的domeElement类的对象无法转换为字符串,我假设我不能只回显$item_pic.Nope,您需要获取节点的url属性。编辑。这是我在修复名称空间后最初尝试的。我使用了:
$item\u pic=$article->getelementsbytagnames('http://search.yahoo.com/mrss/“,”content')->item(0)->getAttribute('url')但是当我在一个非object上调用一个成员函数getAttribute()时,我发现了这个问题。不是每一篇文章都有一个。哦,好吧,那么getAttribute()确实可以工作,如果一篇文章没有图像,那么它会抛出整个脚本?在这种情况下,我将尝试在$item_pic上设置一个“IF”条件,可能类似这样的
IF($item_pic=$article->getelementsbytagnames('http://search.yahoo.com/mrss/“,”content')->item(0)->getAttribute('url'){$image='';}否则{$image='';}
$xml=("http://rapfix.mtv.com/feed");

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

$x = $xmlDoc->getElementsByTagName('item');

foreach($x as $article){
        $item_title = $article->getElementsByTagName('title')->item(0)->nodeValue;
    $item_link = $article->getElementsByTagName('link')->item(0)->nodeValue;
    $item_desc = $article->getElementsByTagName('description')->item(0)->nodeValue;
    $item_pic = $article->getElementsByTagNameNS('http://purl.org/rss/1.0/modules/content/', 'content')->item(0);

        echo ("<strong><a href='".$item_link."' target='_blank'>".$item_title."</a></strong><br />");
    echo ("<div><div class='FloatLeft'><img src='".$item_pic."' width='100' height='100'/></div><div class='FloatLeft'>".$item_desc." - <a href='".$item_link."' target='_blank'>Read More</a></div>^");
}
foreach($x as $article)
{
        $nlContent = $article->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content');
        if( $nlContent->length > 0 )
                $item_pic = $nlContent->item(0)->getAttribute('url');
        else
                $item_pic = '/images/noimageavailable.jpg';
        echo $item_pic . "\n";
}