【发布时间】:2011-05-16 15:02:25
【问题描述】:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<channel>
<item>
<category>Cat1</category>
</item>
<item>
<category>Cat1</category>
</item>
<item>
<category>Cat2</category>
</item>
<item>
<category>Cat3</category>
</item>
</channel>
</root>
我有这个 xml,我如何获得一个项目的最后一个类别而不重复? 我正在尝试:
<?php
$DOMDocument = new DOMDocument( '1.0', 'utf-8' );
$DOMDocument->preserveWhiteSpace = false;
$DOMDocument->load( 'xml.xml' );
$DOMXPath = new DOMXPath( $DOMDocument );
foreach( $DOMXPath->query('.//channel/item/category[last()]/parent::node()') as $Nodes ){
foreach( $Nodes->childNodes as $Node ){
$RSS[ $Node->nodeName ] = $Node->nodeValue;
}
$RSSContents[] = $RSS;
}
echo '<pre>';
print_r( $RSSContents );
但要改写:
Array
(
[0] => Array
(
[category] => Cat1
)
[1] => Array
(
[category] => Cat1
)
[2] => Array
(
[category] => Cat2
)
[3] => Array
(
[category] => Cat3
)
)
我需要退回 cat 1 + 其他物品中的最后一个
【问题讨论】:
-
“我需要退回 cat 1 + 其他物品的最后一个”是什么意思
-
获取最后一个类别:我有 { cat1, cat1( last ), cat2, cat3 } 需要返回 { cat1 ( last ) , cat2, cat3 }
标签: php xml rss domdocument domxpath