Accessing @attribute from SimpleXML

PhpXmlSimplexml

Php Problem Overview


I am having a problem accessing the @attribute section of my SimpleXML object. When I var_dump the entire object, I get the correct output, and when I var_dump the rest of the object (the nested tags), I get the correct output, but when I follow the docs and var_dump $xml->OFFICE->{'@attributes'}, I get an empty object, despite the fact that the first var_dump clearly shows that there are attributes to output.

Anyone know what I am doing wrong here/how I can make this work?

Php Solutions


Solution 1 - Php

Try this

$xml->attributes()->Token

Solution 2 - Php

You can get the attributes of an XML element by calling the attributes() function on an XML node. You can then var_dump the return value of the function.

More info at php.net http://php.net/simplexmlelement.attributes

Example code from that page:

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

Solution 3 - Php

I used before so many times for getting @attributes like below and it was a little bit longer.

$att = $xml->attributes();
echo $att['field'];

It should be more easy and you can get attributes following format only at once:

Standard Way - Array-Access Attributes (AAA)
$xml['field'];

Other alternatives are:

Right & Quick Format
$xml->attributes()->{'field'};

###Wrong Formats

$xml->attributes()->field;
$xml->{"@attributes"}->field;
$xml->attributes('field');
$xml->attributes()['field'];
$xml->attributes->['field'];

Solution 4 - Php

$xml = <<<XML
<root>
<elem attrib="value" />
</root>
XML;

$sxml = simplexml_load_string($xml);
$attrs = $sxml->elem->attributes();
echo $attrs["attrib"]; //or just $sxml->elem["attrib"]

Use SimpleXMLElement::attributes.

Truth is, the SimpleXMLElement get_properties handler lies big time. There's no property named "@attributes", so you can't do $sxml->elem->{"@attributes"}["attrib"].

Solution 5 - Php

You can just do:

echo $xml['token'];

Solution 6 - Php

If you're looking for a list of these attributes though, XPath will be your friend

print_r($xml->xpath('@token'));

Solution 7 - Php

It helped me to convert the result of simplexml_load_file($file) into a JSON Structure and decode it back:

$xml = simplexml_load_file("$token.xml");
$json = json_encode($xml);
$xml_fixed = json_decode($json);

$try1 = $xml->structure->{"@attributes"}['value'];
print_r($try1);

>> result: SimpleXMLElement Object
(
)

$try2 = $xml_fixed->structure->{"@attributes"}['value'];
print_r($try2);

>> result: stdClass Object
(
    [key] => value
)

Solution 8 - Php

Unfortunately I have a unique build (stuck with Gentoo for the moment) of PHP 5.5, and what I found was that

 $xml->tagName['attribute']

was the only solution that worked. I tried all of Bora's methods above, including the 'Right & Quick' format, and they all failed.

The fact that this is the easiest format is a plus, but didn't enjoy thinking I was insane trying all of the formats others were saying worked.

Njoy for what its worth (did I mention unique build?).

Solution 9 - Php

I want to extract string (just Song title and Artist name) from external xml file: https://nostalgicfm.ro/NowOnAir.xml This form of xml:

 <Schedule System="Jazler">
     <Event status="happening" startTime="20:31:20" eventType="song">
        <Announcement Display=""/>
      <Song title="Let It Be ">
       <Artist name="Beatles">
        <Media runTime="265.186"/>
        <Expire Time="20:35:45"/>
       </Artist>
      </Song>
     </Event>
    </Schedule>

I try this code PHP but i don't know how to extract name & title...like "Beatles - Let It Be"

  <?php
    $url = "https://nostalgicfm.ro/NowOnAir.xml";
    $xml = simplexml_load_file($url);
    print_r($xml);
    ?>

Result is an Oject:

 SimpleXMLElement Object ( [@attributes] => Array ( [System] => Jazler ) [Event] => SimpleXMLElement Object ( [@attributes] => Array ( [status] => happening [startTime] => 20:51:21 [eventType] => song ) [Announcement] => SimpleXMLElement Object ( [@attributes] => Array ( [Display] => ) ) [Song] => SimpleXMLElement Object ( [@attributes] => Array ( [title] => If You Were A Sailboat ) [Artist] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => Katie Melua ) [Media] => SimpleXMLElement Object ( [@attributes] => Array ( [runTime] => 228.732 ) ) [Expire] => SimpleXMLElement Object ( [@attributes] => Array ( [Time] => 20:55:09 ) ) ) ) ) ) 

Solution 10 - Php

Resolved it myself:

<?php 
$url = 'https://nostalgicfm.ro/NowOnAir.xml'; 
$xml = simplexml_load_file($url); 
foreach ( $xml->Event->Song->Artist->attributes() as $tag => $value ); 
foreach ( $xml->Event->Song->attributes() as $tag => $value1 ) { 
echo $value." - ".$value1.PHP_EOL; } 
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbenjyView Question on Stackoverflow
Solution 1 - PhpzysoftView Answer on Stackoverflow
Solution 2 - PhpNiels BomView Answer on Stackoverflow
Solution 3 - PhpBoraView Answer on Stackoverflow
Solution 4 - PhpArtefactoView Answer on Stackoverflow
Solution 5 - PhpAlix AxelView Answer on Stackoverflow
Solution 6 - PhpCory CollierView Answer on Stackoverflow
Solution 7 - Phpt2mView Answer on Stackoverflow
Solution 8 - PhpGerard ONeillView Answer on Stackoverflow
Solution 9 - PhppetroView Answer on Stackoverflow
Solution 10 - PhppetroView Answer on Stackoverflow