以下のコードを使用してdescription要素内のimgからsrc属性を取得しようとしています。PHP DOMDocumentを使用して他の要素内の要素から属性を取得する方法
このすべては、これは私が読んでいるのxml estructureある
正しく
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=7; $i++) {
$item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
// Here is where is the mistake
$item_url_img = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->getElementsByTagName('img')->item(0)->getAttribute('src');
$new = new NewFCB();
$new->link = $item_link;
$new->title = $item_title;
$new->description = $item_desc;
$new->imgUrl = $item_url_img;
$listNews[] = $new;
}
を進めています。
img
は の中にありますを
description
の要素の中に入れようとしています。
<item>
<title>Digne, baja por unas molestias en la rodilla</title>
<link>
http://www.sport.es/es/noticias/barca/digne-baja-por-unas-molestias- rodilla-5777073?utm_source=rss-noticias&utm_medium=feed&utm_campaign=barca
</link>
<pubDate>Tue, 31 Jan 2017 13:45:39 +0200</pubDate>
<description>
<p>Lucas Digne no se desplazará con el resto de la plantilla del Barça al Vicente Calderón para la disputa del partido de ida de las semifinales de Copa frente al Atlético Madrid. El defensa francés es baja en la ...</p><a href="http://www.sport.es/es/noticias/barca/digne-baja-por-unas-molestias- rodilla-5777073?utm_source=rss- noticias&utm_medium=feed&utm_campaign=barca">leer la noticia completa</a> <br/>
<img alt="" src="http://estaticos.sport.es/resources/jpg/7/8/jordi-alba-viajara-con-equipo-calderon-1485865523987.jpg"/>
</description>
<guid>
http://www.sport.es/es/noticias/barca/digne-baja-por-unas-molestias- rodilla-5777073?utm_source=rss-noticias&utm_medium=feed&utm_campaign=barca
</guid>
</item>
少し:-) - [[rss] 'はCSS属性セレクタになります。 '@ src'または' attribute :: src'はXpathの場所パスになります。 – ThW
@ThWリフレッシュしていただきありがとうございます。 :-) –