PHP SimpleXML + Get Attribute

前端 未结 7 1281
猫巷女王i
猫巷女王i 2020-12-01 18:32

The XML I am reading looks like this:



    The Big Bang Theory
    http://www.tvrage.com/The_Bi         


        
相关标签:
7条回答
  • 2020-12-01 18:43

    This should work.

    $id = $xml["id"];
    

    Your XML root becomes the root of the SimpleXML object; your code is calling a chid root by the name of 'show', which doesn't exist.

    You can also use this link for some tutorials: http://php.net/manual/en/simplexml.examples-basic.php

    0 讨论(0)
  • 2020-12-01 18:43

    This should work. You need to use attributes with type (if sting value use (string))

    $id = (string) $xml->show->attributes()->id;
    var_dump($id);
    

    Or this:

    $id = strip_tags($xml->show->attributes()->id);
    var_dump($id);
    
    0 讨论(0)
  • 2020-12-01 18:44

    You need to use attributes

    I believe this should work

    $id = $xml->show->attributes()->id;
    
    0 讨论(0)
  • 2020-12-01 18:47

    You need to use attributes() to get the attributes.

    $id = $xml->show->attributes()->id;
    

    You can also do this:

    $attr = $xml->show->attributes();
    $id = $attr['id'];
    

    Or you can try this:

    $id = $xml->show['id'];
    

    Looking at the edit to your question (<show> is your root element), try this:

    $id = $xml->attributes()->id;
    

    OR

    $attr = $xml->attributes();
    $id = $attr['id'];
    

    OR

    $id = $xml['id'];
    
    0 讨论(0)
  • 2020-12-01 18:48

    After you have correctly load the xml file using the SimpleXML objecto you can do a print_r($xml_variable) and you can easily find which attributes you can access. As other users said $xml['id'] also worked for me.

    0 讨论(0)
  • 2020-12-01 18:54

    try this

    $id = (int)$xml->show->attributes()->id;
    
    0 讨论(0)
提交回复
热议问题