douzhiling3166 2017-08-18 07:04 采纳率: 100%
浏览 42
已采纳

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

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;
  }
?>
  • 写回答

3条回答 默认 最新

  • douji8347 2017-08-18 07:24
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b