Good day!

PHP programming language . I convert a string to an XML object:

$xml = simplexml_load_string($xmlstr); 

I display echo var_dump($xml);

I get this result:

 object(SimpleXMLElement)#1 (6) { ["url"]=> object(SimpleXMLElement)#2 (1) { ["@attributes"]=> array(1) { ["domain"]=> string(13) "https://ya.ru" } } ["yaca"]=> object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(1) { ["url"]=> string(13) "https://ya.ru" } } ["tcy"]=> object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(2) { ["rang"]=> string(1) "6" ["value"]=> string(4) "9500" } } ["topics"]=> object(SimpleXMLElement)#5 (1) { ["topic"]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(2) { ["title"]=> string(43) "Π’Π΅ΠΌΠ°: ΠŸΠΎΠΈΡΠΊΠΎΠ²Ρ‹Π΅ систСмы" ["url"]=> string(26) "http://yaca.yandex.ru/yca/" } } } ["textinfo"]=> string(113) " Π’Π΅ΠΌΠ°: ΠŸΠΎΠΈΡΠΊΠΎΠ²Ρ‹Π΅ систСмы Π Π΅Π³ΠΈΠΎΠ½: Россия Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ: ΠžΡ„ΠΈΡ†ΠΈΠ°Π»ΡŒΠ½Ρ‹ΠΉ" ["r1"]=> string(132) "wutwrfslmeextvlpphhotyoutxbmkulbvyuokbuxsocurtftohkircrhsjqjubpnlwfmxuhjgaxyxjrynbllcrqvtobsbtqiraide1d522c48423e52e8f6796dc9f1b3125" } 

I can not figure out how I get the value field value tcy . I appeal as an array, but always an empty result.

Added source XML:

 <urlinfo> <url domain="https://yandex.ru"> <![CDATA[ / ]]> </url> <yaca url="https://yandex.ru"/> <tcy rang="6" value="380000"/> <topics></topics> <textinfo></textinfo> <r1> uvyerddwhdmcirwatmowxmyxpktyjugqbnhwbufhgmfvugvofkvjbfkaykbtndygknieqitmtjousnxtjjospvednwjemswehrlo68dda255ecb7540b7f383df508b14e8f </r1> </urlinfo> 

Thank!

  • one
    source XML would be better led - teran
  • @teran added. - VladislavMSK

2 answers 2

I can be mistaken with positioning without seeing xml, but attribute values ​​are obtained simply by specifying their name as for an associative array. Like this

 echo $xml->tcy['value'] 

Learn more about using Simplexml.

  • Thanks It works! - VladislavMSK
  • Fine! Good luck! - splash58
 function xmlAttribute($object, $attribute){ if(isset($object[$attribute])) return (string) $object[$attribute]; else return null; } $tcyValue=xmlAttribute($xml->tcy->attributes(),"value") 

If the structure is not messed up. The whole point is that you cannot directly contact the sv-vu object @attributes . Once even it turned out through a variable like, like:

 $attributes="@attributes"; echo $xml->tcy->$attributes['value']; 

But then somehow it didn't work out, and I began to always use the option above.

  • Thank you for your reply! - VladislavMSK
  • @DaemonHK, in general, it’s very strange about how something went wrong. Only in the presence of a namespace can I imagine this - splash58