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.
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";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.