Re: PHP 5: SimpleXML attributes

From: Date: Wed, 07 Apr 2004 20:41:10 +0000
Subject: Re: PHP 5: SimpleXML attributes
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-182703@lists.php.net to get a copy of this message
Red Wingate wrote:
<?xml version="1.0" encoding="iso-8859-1" ?> <config> <lib_error>
    <param name="error_logfile_write" type="boolean">FALSE</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?
[...]


« previous php.general (#182703) next »