简体   繁体   中英

How to display XML nodes that have the same name?

I have an XML blog feed that I am displaying on a Wordpress. Here is a simple representation of the XML nodes:

<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. 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.

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. If you just add a foreach you can add them all...

$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.

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

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

var_dump($results);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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