Jump to content

Better alternative to simplexml


devWhiz

Recommended Posts

Well I cant get simplexml to parse out the xml I need correctly, I tried to call out multi entrys in the xml and print them out in CLI and I only get the first letter of a couple of the strings..

 

$LoadXML = file_get_contents("xmldoc.xml");
$xml = simplexml_load_string($LoadXML);
foreach($xml->xml->user as $entry)
{
    $ID = $entry->id;
    $name = $entry->name;
    $age = $entry->age;
}
for($x=0; $x!=count($entry)-1; $x++)
{
echo $ID[$x]."\n";
echo $name[$x]."\n";
echo $age[$x]."\n";
}



 

The code above only echos the last entry of the user.. here is the xml

 


<outer>
<xml>
	<user>
		<id>123456</id>
		<name>Clueless</name>
		<age>18</age>
	</user>
	<user>
		<id>234566</id>
		<name>AnotherName</name>
		<age>21</age>
	</user>
	<user>
		<id>1232426</id>
		<name>YetAnother</name>
		<age>22</age>
	</user>
</xml>
</outer>

 

outputs

 

1232426 
YetAnother 
22 

 

Link to comment
https://forums.phpfreaks.com/topic/238509-better-alternative-to-simplexml/
Share on other sites

You want to tell PHP that the variables will hold arrays using

$ID = array(); $name = array(); $age = array();

Declare that before you populate them.

 

Populate them using this instead.

    $ID[] = $entry->id;
    $name[] = $entry->name;
    $age[] = $entry->age;

 

Leaving the key blank will tell the engine to automatically call array_push() and add the value to a new key at the end of the array.

 

Your script should work fine.

 

Not a simplexml issue.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.