1

I am trying to echo out each <TEXT> block in my XML, minus the CDATA.

But for some reason I am only getting the first one.

What am I doing wrong?

My 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>

My 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();
}
?>

My Output:

@@wrongopia@@ was admitted to the World Assembly.
LSD
  • 525
  • 1
  • 6
  • 12
  • Note: contrary to many references you may find online, you do not need to use `LIBXML_NOCDATA`. Try it without and your code will work just fine. :) – IMSoP Jan 21 '15 at 13:25

1 Answers1

2

Your current code iterates all TEXT elements of the first EVENT element (since you are dereferencing a property of ->EVENT it is implicitly interpreted as ...->(EVENT[0])->....)

You want to iterate all EVENT elements and print their respective TEXT elements.

foreach( $xml->HAPPENINGS->EVENT as $event ) {
   echo $event->TEXT, "\r\n";
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226