problem with simpleXMLElement
Here is my PHP code
$xml= new SimpleXmlElement($rawxml);
foreach($xml->children()->children() AS $key){
$id = $xml->{"id"};
$name = $xml->{"screen_name"};
$profimg = $xml->{"profile_image_url"};
echo "$id, $name, $profimg";
}
$next = $xml->{"next_link"};
echo "index.php?".$next;
Here is the structure of my xml
<?xml version="1.0" encoding="UTF-8"?>
<users_list>
<users type="array">
<user>
<id>44444</id>
<screen_name>Some Name</screen_name>
<profile_image_url>http://www.website.com/picture.jpg</profile_image_url>
</user>
<user>
<id>555</开发者_JAVA百科id>
<screen_name>Bob</screen_name>
<profile_image_url>http://www.website.com/picture2.jpg</profile_image_url>
</user>
<user>
<id>666666</id>
<screen_name>Frank</screen_name>
<profile_image_url>http://www.website.com/picture3.jpg</profile_image_url>
</user>
</users>
<next_link>44444</next_link>
</users_list>
Im trying to assign the values of the field to variables and then echo them. Then at the bottom echo the nextlink.
I dont get any errors, but it just shows the first field over and over, and doesnt output the nextlink.
You’re using $key
in the foreach
expression but $xml
inside the foreach
body.
I would also prefer an XPath expression rather than your children of the children of the root way:
foreach ($xml->xpath('/users_list/users/user') as $user) {
$id = $user->id;
$name = $user->screen_name;
$profimg = $user->profile_image_url;
echo "$id, $name, $profimg";
}
精彩评论