Accessing @attribute from SimpleXML

前端 未结 8 1262
后悔当初
后悔当初 2020-11-22 00:33

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 wh

相关标签:
8条回答
  • 2020-11-22 01:33

    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
    )
    
    0 讨论(0)
  • 2020-11-22 01:35

    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";
    }
    
    0 讨论(0)
提交回复
热议问题