如何显示XML个同名节点?

How to display XML nodes that have the same name?

我有一个要在 Wordpress 上显示的 XML 博客提要。这是 XML 个节点的简单表示:

<item>
    <title>
      Some title
    </title>
    <description>
      Some description
    </description>
    <category>
      category 1
    </category>
    <category>
      category 2
    </category>
    <category>
      category 3
    </category>
 </item>

所以您会注意到上面的类别节点显示了 3 次,但名称始终相同。因此,当我使用下面的 PHP 在循环中显示 XML 节点时,我只能获得类别节点之一,因为其余节点不是唯一的。

有谁知道如何显示所有这些类别节点吗???

<?php
    $rss = new DOMDocument();
    $rss->load('http://blog.com/rss.xml');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
      $item = array (
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'category' => $node->getElementsByTagName('category')->item(0)->nodeValue,
        );
      array_push($feed, $item);
    }
  ?>

<?php
  $limit = 3;
  for($x=0;$x<$limit;$x++) {
  $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
  $description = $feed[$x]['desc'];
  $category = $feed[$x]['category'];

  echo $title;
  echo $desc;
  echo $category;
  }
?>

为每个项目保存类别而不是一个类别:

<?php

function domNodeList2Array($node_list)
{
    $nodes = [];
    foreach ($node_list as $node) {
        $nodes[] = $node;
    }
    return $nodes;
}

$rss = new DOMDocument();
$rss->load('rss.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = [
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'categories' => array_map(function ($item) {
            return $item->nodeValue;
        }, domNodeList2Array($node->getElementsByTagName('category')))];
    array_push($feed, $item);
}


$limit = 1;
for ($x = 0; $x < $limit; $x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $description = $feed[$x]['desc'];
    $categories = $feed[$x]['categories'];

    echo $title;
    echo $description;
    foreach ($categories as $category) {
        echo $category;
    }
}

您可以使用 "simplexml_load_file" 来 return 数组。

$xml = simplexml_load_file('http://blog.com/rss.xml');

$results = $xml->xpath('//category');

var_dump($results);

因为您总是选择第一个元素(使用 [0]),所以这将始终选择第一个类别。如果您只添加一个 foreach,您可以将它们全部添加...

$rss = new DOMDocument ();
$rss->load ( 'rss.xml' );
$feed = array ();
foreach ( $rss->getElementsByTagName ( 'item' ) as $node ) {
    $item = array (
            'title' => $node->getElementsByTagName ( 'title' )->item ( 0 )->nodeValue,
            'desc' => $node->getElementsByTagName ( 'description' )->item ( 0 )->nodeValue,
            'category' => array());
    foreach ( $node->getElementsByTagName ( 'category' ) as $cat ) {
        $item['category'][] = $cat->nodeValue;
    }
    array_push ( $feed, $item );
}

foreach($feed as $feedItem ) {
    $title = str_replace ( ' & ', ' &amp; ', $feedItem ['title'] );
    $description = $feedItem ['desc'];
    $category = $feedItem ['category'];

    echo $title;
    echo $description;
    foreach ( $category as $cat )   {
        echo "Category:".$cat.PHP_EOL;
    }
}