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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致