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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.