[PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread Dan Phiffer
Hello,

I have what I hope to be a simple question about SimpleXML. Is it 
possible to get the attributes of a SimpleXML node as an associative array?

Consider the following:

?php
$sxe = simplexml_load_string('root attr=value/');
print_r($sxe-attributes());
// Gives: simplexml_element Object ( [0] = value )
?
Is there some way to find out that 'value' is the attribute called 
'attr'? In other words, can I iterate over SimpleXML attributes without 
losing track of what their attribute name is? Or is this a case where I 
should switch over to DOM?

Thanks!
-Dan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread Red Wingate
There is a way, but somehow i don't like it though ( but you
can find this in the documentation ):
This is config.xml:

?xml version=1.0 encoding=iso-8859-1 ?
config
  lib_error
param name=error_logfile_write type=booleanFALSE/param

  /lib_error
  
/config
And my Testfile:

$xml = simplexml_load_file( 'config.xml' );

foreach ( $xml-lib_error-param AS $id = $param ) {
echo $param['name'] ; // will output 'boolean' !
}
 -- red

[...]
Hello,

I have what I hope to be a simple question about SimpleXML. Is it 
possible to get the attributes of a SimpleXML node as an associative array?

Consider the following:

?php
$sxe = simplexml_load_string('root attr=value/');
print_r($sxe-attributes());
// Gives: simplexml_element Object ( [0] = value )
?
Is there some way to find out that 'value' is the attribute called 
'attr'? In other words, can I iterate over SimpleXML attributes without 
losing track of what their attribute name is? Or is this a case where I 
should switch over to DOM?
[...]

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread John W. Holmes
From: Dan Phiffer [EMAIL PROTECTED]

 I have what I hope to be a simple question about SimpleXML. Is it
 possible to get the attributes of a SimpleXML node as an associative
array?

 Consider the following:

 ?php
 $sxe = simplexml_load_string('root attr=value/');
 print_r($sxe-attributes());
 // Gives: simplexml_element Object ( [0] = value )
 ?

 Is there some way to find out that 'value' is the attribute called
 'attr'? In other words, can I iterate over SimpleXML attributes without
 losing track of what their attribute name is? Or is this a case where I
 should switch over to DOM?

Have you seen Example 4 here: http://us2.php.net/manual/en/ref.simplexml.php

Not sure if that helps or not, though, but it looks like it's already an
associative array.

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread Dan Phiffer
Red Wingate wrote:

?xml version=1.0 encoding=iso-8859-1 ?
config
  lib_error
param name=error_logfile_write type=booleanFALSE/param

  /lib_error
  
/config

$xml = simplexml_load_file( 'config.xml' );

foreach ( $xml-lib_error-param AS $id = $param ) {
echo $param['name'] ; // will output 'boolean' !
}
I think you mean that would output 'error_logfile_write'. However, what 
I'm asking is if it's possible to iterate over the SimpleXML node 
attributes 'name' and 'type', without knowing ahead of time that 'name' 
and 'type' are expected associative keys. There is an attributes() class 
method, but that just returns a numerically indexed array of the 
attribute values:

Array (
[0] = 'error_logfile_write',
[1] = 'boolean'
)
What I want is:

Array (
['name'] = 'error_logfile_write',
['type'] = 'boolean'
)
Thanks!
-Dan
Hello,

I have what I hope to be a simple question about SimpleXML. Is it 
possible to get the attributes of a SimpleXML node as an associative 
array?

Consider the following:

?php
$sxe = simplexml_load_string('root attr=value/');
print_r($sxe-attributes());
// Gives: simplexml_element Object ( [0] = value )
?
Is there some way to find out that 'value' is the attribute called 
'attr'? In other words, can I iterate over SimpleXML attributes 
without losing track of what their attribute name is? Or is this a 
case where I should switch over to DOM?
[...]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread Dan Phiffer
John W. Holmes wrote:

Have you seen Example 4 here: http://us2.php.net/manual/en/ref.simplexml.php

Not sure if that helps or not, though, but it looks like it's already an
associative array.
It certainly looks like an associative array, but the array_keys() 
function chokes on it:

Warning: array_keys() [function.array-keys]: The first argument should 
be an array in D:\...\test.php on line 5

Explicitely casting it as an array doesn't work either. I guess I want 
something like example 4, but without having to hardcode in the 
$rating['type']. Something more like:

foreach ($xml-movie[0]-rating as $rating) {
foreach ($rating-attributes() as $key = $value) {
if ($key == 'type') {
switch ($rating['type']) {
...
}
}
}
}
Thanks,
-Dan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread Red Wingate
So, this one will work out quite well ( maybe one should take
a look at the documentation of attributes() :-)
foreach ( $xml-lib_error-param AS $param ) {
$n = 0 ;
while ( is_object ( $xml-lib_error-param[$n] ) ) {
foreach( $xml-lib_error-param[$n]-attributes() AS $a = $b ){
echo $a .' -- '. $b .'br';
}
$n++;
}
}
 -- red

Dan Phiffer wrote:

Red Wingate wrote:

?xml version=1.0 encoding=iso-8859-1 ?
config
  lib_error
param name=error_logfile_write type=booleanFALSE/param

  /lib_error
  
/config
 

$xml = simplexml_load_file( 'config.xml' );

foreach ( $xml-lib_error-param AS $id = $param ) {
echo $param['name'] ; // will output 'boolean' !
}


I think you mean that would output 'error_logfile_write'. However, what 
I'm asking is if it's possible to iterate over the SimpleXML node 
attributes 'name' and 'type', without knowing ahead of time that 'name' 
and 'type' are expected associative keys. There is an attributes() class 
method, but that just returns a numerically indexed array of the 
attribute values:

Array (
[0] = 'error_logfile_write',
[1] = 'boolean'
)
What I want is:

Array (
['name'] = 'error_logfile_write',
['type'] = 'boolean'
)
Thanks!
-Dan
Hello,

I have what I hope to be a simple question about SimpleXML. Is it 
possible to get the attributes of a SimpleXML node as an associative 
array?

Consider the following:

?php
$sxe = simplexml_load_string('root attr=value/');
print_r($sxe-attributes());
// Gives: simplexml_element Object ( [0] = value )
?
Is there some way to find out that 'value' is the attribute called 
'attr'? In other words, can I iterate over SimpleXML attributes 
without losing track of what their attribute name is? Or is this a 
case where I should switch over to DOM?


[...]


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread Dan Phiffer
Red Wingate wrote:

So, this one will work out quite well ( maybe one should take
a look at the documentation of attributes() :-)
Wow I can't believe I missed that. Hehe.

I guess the lesson here is don't always trust what you get from print_r().

Thanks for the responses,
-Dan

foreach ( $xml-lib_error-param AS $param ) {
$n = 0 ;
while ( is_object ( $xml-lib_error-param[$n] ) ) {
foreach( $xml-lib_error-param[$n]-attributes() AS $a = $b ){
echo $a .' -- '. $b .'br';
}
$n++;
}
}
 -- red

Dan Phiffer wrote:

Red Wingate wrote:

?xml version=1.0 encoding=iso-8859-1 ?
config
  lib_error
param name=error_logfile_write type=booleanFALSE/param

  /lib_error
  
/config


 

$xml = simplexml_load_file( 'config.xml' );

foreach ( $xml-lib_error-param AS $id = $param ) {
echo $param['name'] ; // will output 'boolean' !
}


I think you mean that would output 'error_logfile_write'. However, 
what I'm asking is if it's possible to iterate over the SimpleXML node 
attributes 'name' and 'type', without knowing ahead of time that 
'name' and 'type' are expected associative keys. There is an 
attributes() class method, but that just returns a numerically indexed 
array of the attribute values:

Array (
[0] = 'error_logfile_write',
[1] = 'boolean'
)
What I want is:

Array (
['name'] = 'error_logfile_write',
['type'] = 'boolean'
)
Thanks!
-Dan
Hello,

I have what I hope to be a simple question about SimpleXML. Is it 
possible to get the attributes of a SimpleXML node as an associative 
array?

Consider the following:

?php
$sxe = simplexml_load_string('root attr=value/');
print_r($sxe-attributes());
// Gives: simplexml_element Object ( [0] = value )
?
Is there some way to find out that 'value' is the attribute called 
'attr'? In other words, can I iterate over SimpleXML attributes 
without losing track of what their attribute name is? Or is this a 
case where I should switch over to DOM?


[...]



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] PHP 5: SimpleXML attributes

2004-04-07 Thread Red Wingate
Guess it was quite luck i saw the little note in the documentation:

Note: SimpleXML has made a rule of adding iterative properties to most 
methods. They cannot be viewed using var_dump() or anything else which 
can examine objects.

Guess i will have to take a look at my config parser again :-)

 -- red

Dan Phiffer wrote:

Red Wingate wrote:

So, this one will work out quite well ( maybe one should take
a look at the documentation of attributes() :-)


Wow I can't believe I missed that. Hehe.

I guess the lesson here is don't always trust what you get from print_r().

Thanks for the responses,
-Dan

foreach ( $xml-lib_error-param AS $param ) {
$n = 0 ;
while ( is_object ( $xml-lib_error-param[$n] ) ) {
foreach( $xml-lib_error-param[$n]-attributes() AS $a = $b ){
echo $a .' -- '. $b .'br';
}
$n++;
}
}
 -- red

Dan Phiffer wrote:

Red Wingate wrote:

?xml version=1.0 encoding=iso-8859-1 ?
config
  lib_error
param name=error_logfile_write type=booleanFALSE/param

  /lib_error
  
/config


 

$xml = simplexml_load_file( 'config.xml' );

foreach ( $xml-lib_error-param AS $id = $param ) {
echo $param['name'] ; // will output 'boolean' !
}




I think you mean that would output 'error_logfile_write'. However, 
what I'm asking is if it's possible to iterate over the SimpleXML 
node attributes 'name' and 'type', without knowing ahead of time that 
'name' and 'type' are expected associative keys. There is an 
attributes() class method, but that just returns a numerically 
indexed array of the attribute values:

Array (
[0] = 'error_logfile_write',
[1] = 'boolean'
)
What I want is:

Array (
['name'] = 'error_logfile_write',
['type'] = 'boolean'
)
Thanks!
-Dan
Hello,

I have what I hope to be a simple question about SimpleXML. Is it 
possible to get the attributes of a SimpleXML node as an 
associative array?

Consider the following:

?php
$sxe = simplexml_load_string('root attr=value/');
print_r($sxe-attributes());
// Gives: simplexml_element Object ( [0] = value )
?
Is there some way to find out that 'value' is the attribute called 
'attr'? In other words, can I iterate over SimpleXML attributes 
without losing track of what their attribute name is? Or is this a 
case where I should switch over to DOM?




[...]





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php