【问题标题】:How to get an attribute from an element within other one element using PHP DOMDocument如何使用 PHP DOMDocument 从另一个元素中的元素获取属性
【发布时间】:2017-01-31 22:02:47
【问题描述】:

我正在尝试使用下面的代码从描述元素的 img 中获取属性 src。

一切正常

$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;
}

这是我正在阅读的 xml 结构。 img 在里面 我正在尝试在description 元素中获取img

<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&#160;no se desplazará con el resto de la plantilla     del&#160;Barça&#160;al&#160;Vicente Calderón&#160;para la disputa del partido     de ida de las semifinales de&#160;Copa&#160;frente al&#160;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>

【问题讨论】:

    标签: php xml rss domdocument


    【解决方案1】:

    使用 Xpath 表达式获取节点和值:

    $document = new DOMDocument();
    $document->loadXml($xml);
    $xpath = new DOMXpath($document);
    
    foreach ($xpath->evaluate('//item') as $item) {
      $row = [
        'title' => $xpath->evaluate('string(title)', $item),
        'link' => $xpath->evaluate('string(link)', $item),
        'description' => $xpath->evaluate('string(description)', $item),
        'image-src' => $xpath->evaluate('string(description/img/@src)', $item)
      ];
      var_dump($row);
    }
    

    但是在 RSS 描述中通常包含一个 HTML sn-p 作为文本节点或 cdata 部分。在这种情况下,您必须将其加载到片段节点并在其上使用 Xpath。

    【讨论】:

      【解决方案2】:

      我不完全确定,但我认为,错误在于

      ->getElementsByTagName('description')->item(0)->childNodes->item(0)->
      

      第二个item(0) 为您提供p 元素,该元素在img 元素之前关闭,因此不能包含它。


      也许,您想考虑XPath,它可以为您提供src 属性

      /item/description/img/@src
      

      或类似的(我的 XPath-fu 有点生疏)。

      【讨论】:

      • 一点点 :-) - [rss] 将是一个 CSS 属性选择器。 @srcattribute::src 将是 Xpath 位置路径。
      猜你喜欢
      • 1970-01-01
      • 2012-05-18
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多