dovhpmnm31216 2013-05-15 22:20
浏览 29

RSS中的PHP getElementsByTagName忽略具有domain属性的类别标记

I am trying to parse an RSS file using the following PHP:

$doc = new DOMDocument();
$doc->load($address);
$feedArr = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array ( 
  'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
  'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
  'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
  );
foreach($node->getElementsByTagName('category') as $catNode) {
$itemRSS['category'][] = $catNode->nodeValue;
}
}

It works fine except if the category node has a domain attribute. When it does, it seems to completely ignore it the category.

Any ideas why?

  • 写回答

1条回答 默认 最新

  • donh61500 2013-05-15 23:26
    关注

    You can access XML attributes like this:

    'whatever' => $node->getElementsByTagName('node')->getAttribute('category')
    

    So I presume your should just amend your loop to this:

    foreach($node->getElementsByTagName('node')->getAttribute('category') as $catNode) {
    }
    

    Although I may be a bit off on the syntax as I haven't coded in PHP for a while, although getAttribute is definitely what you are wanting.

    Actually, there is a good tutorial here using Simplexml function in PHP: http://webtutsdepot.com/2009/07/26/how-to-parse-xml-with-php-part2-xml-with-atrributes/

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大