PHP SimpleXML ForEach

PHP SimpleXML ForEach

我正在尝试回显我的 XML 中的每个 <TEXT> 块,减去 CDATA。

但出于某种原因,我只得到了第一个。

我做错了什么?

我的XML:

<WORLD>
<HAPPENINGS>
<EVENT id="30978968">
<TIMESTAMP>1421724317</TIMESTAMP>
<TEXT>
<![CDATA[ @@wrongopia@@ was admitted to the World Assembly. ]]>
</TEXT>
</EVENT>
<EVENT id="30978955">
<TIMESTAMP>1421724294</TIMESTAMP>
<TEXT>
<![CDATA[ @@wrongopia@@ applied to join the World Assembly. ]]>
</TEXT>
</EVENT>
<EVENT id="30978506">
<TIMESTAMP>1421723233</TIMESTAMP>
<TEXT>
<![CDATA[ @@panebo@@ applied to join the World Assembly. ]]>
</TEXT>
</EVENT>
<EVENT id="30978469">
<TIMESTAMP>1421723119</TIMESTAMP>
<TEXT>
<![CDATA[ @@spoonville@@ applied to join the World Assembly. ]]>
</TEXT>
</EVENT>
<EVENT id="30978414">
<TIMESTAMP>1421722933</TIMESTAMP>
<TEXT>
<![CDATA[ @@sapari@@ was admitted to the World Assembly. ]]>
</TEXT>
</EVENT>
<EVENT id="30978380">
<TIMESTAMP>1421722798</TIMESTAMP>
<TEXT>
<![CDATA[
@@fitzserland@@ was admitted to the World Assembly.
]]>
</TEXT>
</EVENT>
<EVENT id="30978366">
<TIMESTAMP>1421722742</TIMESTAMP>
<TEXT>
<![CDATA[
@@fitzserland@@ applied to join the World Assembly.
]]>
</TEXT>
</EVENT>
</HAPPENINGS>
</WORLD>

我的PHP:

<?php
$x = 0;
function waAdmits() {
$file = "https://www.nationstates.net/cgi-bin/api.cgi?q=happenings;filter=member;limit=7";
$xml = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
foreach( $xml->HAPPENINGS->EVENT->TEXT as $waAdmitNations ) {
echo $waAdmitNations;
} 
}
while($x<1)
{
$x++;
waAdmits();
}
?>

我的输出:

@@wrongopia@@ was admitted to the World Assembly.

您当前的代码迭代 first EVENT 元素的所有 TEXT 元素(因为您正在取消引用 ->EVENT 的 属性,它被隐式解释为 ... ->(事件[0])->....)

您想迭代所有 EVENT 元素并打印它们各自的 TEXT 元素。

foreach( $xml->HAPPENINGS->EVENT as $event ) {
   echo $event->TEXT, "\r\n";
}