简体   繁体   English

如何显示具有相同名称的XML节点?

[英]How to display XML nodes that have the same name?

I have an XML blog feed that I am displaying on a Wordpress. 我有一个要显示在Wordpress上的XML博客供稿。 Here is a simple representation of the XML nodes: 这是XML节点的简单表示:

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

So you'll notice above the category node is displayed 3 times but always has the same name. 因此,您会注意到类别节点上方显示了3次,但始终具有相同的名称。 So when I'm using the below PHP to display the XML nodes in a loop I can only get one of the category nodes as the rest aren't unique. 因此,当我使用下面的PHP在循环中显示XML节点时,我只能得到类别节点之一,因为其余节点不是唯一的。

Does anyone know how I can display all of those category nodes please??? 有谁知道我如何显示所有这些类别节点?

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

Saving categories instead of one category for each item: 保存类别,而不是为每个项目保存一个类别:

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

As your always taking the first element ( using [0] ) this will always pick the first category. 由于您始终选择第一个元素(使用[0] ),因此始终选择第一个类别。 If you just add a foreach you can add them all... 如果您仅添加一个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;
    }
}

You can use "simplexml_load_file" to return an array. 您可以使用“ simplexml_load_file”返回一个数组。

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

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

var_dump($results);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM