|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-02-27 09:43 UTC] wb at pro-net dot co dot uk
Description:
------------
When using simpleXML you are unable to fetch some information. The information is displayed with print_r() but you can't get it it directly.
Use the example provided as an example.
Reproduce code:
---------------
<?php
header('Content-type: text/plain');
$xmlstr = '<?xml version="1.0"?>
<access>
<user>
<login>user1</login>
<password>letMeIn</password>
<site>www.pro-net.co.uk</site>
<site>www.example.com</site>
</user>
<user>
<login>user2</login>
<password>myPassword</password>
<site>www.pro-net.co.uk</site>
</user>
</access>';
$nl = "\r\n";
$xml = simplexml_load_string($xmlstr);
print('Current PHP version is : '.phpversion().$nl.$nl);
print('$xml is:'.$nl);
print_r($xml);
print($nl.$nl);
// Test authentication to get the correct user information
$login = 'user1';
$password = 'letMeIn';
print($nl.$nl.'Trying to authenticate "'.$login.'" with password "'.$password.'".'.$nl.$nl);
$isAuth = false;
foreach($xml->user as $user){
if(utf8_decode($user->login) == $login && utf8_decode($user->password) == $password){
$isAuth = true;
break;
}
}
if(!$isAuth){
print($nl.$nl.'Invalid User.'.$nl);
die();
}
// So lets output the variables to see what we have...
print('$user is:'.$nl);
print_r($user);
print($nl.$nl);
print('$user->site is:'.$nl);
print_r($user->site);
print($nl.$nl);
print('$user->site[0] is:'.$nl);
print_r($user->site[0]);
print($nl.$nl);
?>
Expected result:
----------------
$xml is:
simplexml_element Object
(
[user] => Array
(
[0] => simplexml_element Object
(
[login] => user1
[password] => letMeIn
[site] => Array
(
[0] => www.pro-net.co.uk
[1] => www.example.com
)
)
[1] => simplexml_element Object
(
[login] => user2
[password] => myPassword
[site] => www.pro-net.co.uk
)
)
)
Trying to authenticate "user1" with password "letMeIn".
$user is:
simplexml_element Object
(
[login] => user1
[password] => letMeIn
[site] => Array
(
[0] => www.pro-net.co.uk
[1] => www.example.com
)
)
$user->site is:
Array
(
[0] => www.pro-net.co.uk
[1] => www.example.com
)
$user->site[0] is:
www.pro-net.co.uk
Actual result:
--------------
$xml is:
simplexml_element Object
(
[user] => Array
(
[0] => simplexml_element Object
(
[login] => user1
[password] => letMeIn
[site] => Array
(
[0] => www.pro-net.co.uk
[1] => www.example.com
)
)
[1] => simplexml_element Object
(
[login] => user2
[password] => myPassword
[site] => www.pro-net.co.uk
)
)
)
Trying to authenticate "user1" with password "letMeIn".
$user is:
simplexml_element Object
(
[login] => user1
[password] => letMeIn
[site] => Array
(
[0] => www.pro-net.co.uk
[1] => www.example.com
)
)
$user->site is:
Array
(
[0] => simplexml_element Object
(
)
[1] => simplexml_element Object
(
)
)
$user->site[0] is:
simplexml_element Object
(
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 23:00:01 2025 UTC |
Thanks for your reply. I have been playing around as well as reading some threads on the DEV mailing list. I think the main issue is that the simpleXML documentation in the online manual is misleading and needs to be updated, this i feel would probably solve 80% of the issues :) But the other, and to me more important, issue is the inconsistancy when using print_r() or var_dump(). They should always refer to the items as a simplexml_element Object and never as a string. The thing to remember is that the quickest way to find out what a vairable is is to just print_r() or var_dump() it. Now i feel that most people will be doing this to try to get an understanding of the new PHP5 objects, they want to see quickly what is available, and when they see a value as a string they expect to be able to use it as a string and not have it turn out to be an object later on. Alos the fact that it looks like an empty object when it is a simplexml_element dosen't help either. I have no problem with haveing to cast the variable as a string as it does show exactly how the object is being used in that situation and rightfully "a string" != $anElement. What about haveing the following methods to the simplexml_element Object: ->setText($string) ->getText() Then people could use these to get and set the 'text' value for that element. I added the setValue() method because from the documentation i can't see how one would set it anyway. Anyway those are my ideas and i would be interested to find out what people think.