“At sign” @ in SimpleXML object?

2019-01-12 08:53发布

问题:

This is the output of print_r() run on a typical SimpleXMLElement object:

SimpleXMLElement Object
(
    [@attributes] => Array
        (

        )
)

What does the @ sign mean?

回答1:

This is a SimpleXMLElement object. The '@attributes' row is an internal representation of the attributes from the XML element. Use SimpleXML's functions to get data from this object rather than interacting with it directly.



回答2:

All those answers about error control are incorrect. The @ doesn't mean anything. That's how the property is called internally, but do not rely on this. Do not rely on print_r() or var_dump() when dealing with SimpleXML. SimpleXML does a lot of "magical" things that are not correctly represented by print_r() and var_dump().

If you need to know what's "inside" a XML fragment, just use ->asXML() on it.



回答3:

Sorry, can't comment as a guest but for anyone else who ends up here like I did... I am creating my own Joomla form fields and Joomla creates a very 'interesting' object of all sorts of things. Now, I didn't want to become a SimpleXML expert, all I wanted was the original label text which was squirrelled away in @attributes.

After a bit of "hmmm, I wonder if this works?"™ I found this is the easiest way of accessing these values:

var_dump($simpleXMLObject);

/* Result */
object(SimpleXMLElement)
  public '@attributes' => 
    array (size=3)
      'name' => string 'awesome'
      'label' => string 'Awesome Label'
      'type' => string 'typeOfAwesome'

echo $simpleXMLObject->attributes()->label; // Awesome Label

$simpleXMLObject->attributes()->label = 'Different Day, Different Awesome';
echo $simpleXMLObject->attributes()->label; // Different Day, Different Awesome 

They were not lying. It really is simple.



回答4:

I don't have enough reps to comment on user3098738... but wanted to validate his response. It really is that simple. Any time you run in to @attributes in SimpleXML... use

$simpleXMLObject->attributes()
$simpleXMLObject->key->attributes()


回答5:

I am working with an HTTP API that gives out only XML formatted data. So first I loaded it into SimpleXML and was also puzzled by the @attributes issue.. how do I get at the precious data it contains? print_r() confused me.

My solution was to create an array and an iterator variable at 0. Loop through a SimpleXML object with foreach and get at the data with the attribues() method and load it into my created array. Iterate before foreach loop ends.

So print_r() went from showing this:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [ID] => 1
            [First] => John
            [Last] => Smith
        )
)

To a much more usable normal array. Which is great because I wanted the option to quickly convert array into json if needed.

My solution in code:

$obj = simplexml_load_string($apiXmlData);
$fugly = $obj->Deeply->Nested->XML->Data->Names;
$people = array();
$i = 0;
foreach($fugly as $val)
{
  $people[$i]['id'] += $val->attributes()->ID;
  $people[$i]['first'] = "". $val->attributes()->First;
  $people[$i]['last'] = "". $val->attributes()->Last;
  $i++;
}

Quick note would be PHP's settype() function is weird/buggy, so I added the + to make sure ID is an integer and added the quotes to make sure the name is string. If there isn't a variable conversion of some kind, you're going to be loading SimpleXML objects into the array you created.

Final result of print_r():

Array
(
   [0] => Array
    (
        [id] => 1
        [first] => John
        [last] => Smith
    )

   [1] => Array
    (
        [id] => 2
        [first] => Jane
        [last] => Doe
    )
)


标签: php simplexml