1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to process an SimpleXMLELement object -- its behavior seems a little strange

Discussion in 'PHP' started by winterheat, Apr 23, 2009.

  1. #1
    I wonder why the SimpleXMLELement object seems weird, that when we var_dump($entry):


    object(SimpleXMLElement)#4 (8) {
      ["id"]=>
      string(53) "http://gdata.youtube.com/feeds/api/videos/YkA31t0n1Ew"
      ["published"]=>
      string(24) "2005-12-24T05:14:40.000Z"
    
      ...
    
      ["title"]=>
      string(46) "two chinese boys:bu de bu ai(不得不愛)"
    
    }
    Code (markup):
    so when we var_dump($entry->id), we'd expect it is a string, but no, we actually get another SimpleXMLElement object instead of a string


    object(SimpleXMLElement)#5 (1) {
      [0]=>
      string(53) "http://gdata.youtube.com/feeds/api/videos/YkA31t0n1Ew"
    }
    Code (markup):

    and the same, if we var_dump($entry->title), we also get another SimpleXMLElement object

    object(SimpleXMLElement)#5 (2) {
      ["@attributes"]=>
      array(1) {
        ["type"]=>
        string(4) "text"
      }
      [0]=>
      string(46) "two chinese boys:bu de bu ai(不得不愛)"
    }
    Code (markup):
    so i just wonder, how come it behaves like this, and how do we get the "id" and "title" properly? thanks very much.
     
    winterheat, Apr 23, 2009 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I forgot exactly why it does that (or I didn't know in the first place) but you can use typecasting:

    echo (string) $entry->title;
    PHP:
     
    decepti0n, Apr 23, 2009 IP
  3. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you're misunderstanding how objects work...if you var_dump the object then it's gonna return the object+anything within you specify; i'm assuming $entry was already an instatiated SimpleXML object, so instead try print_r($entry) or echo $entry['title'] etc...let me know how it goes :)
     
    szalinski, Apr 23, 2009 IP
  4. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    same thing. i used print_r():


    print_r($ns_media);
    print_r("foo " . $ns_media['title'] . " bar");
    print_r("foo2 " . $ns_media->title . " bar2");


    and they give


    SimpleXMLElement Object
    (

    ...

    [title] => Saving Our Economy: What'$ Next?
    [description] => It's the economic crisis of the century; our entire nation is at risk. Who's to blame and how do we fix it? FOX Business anchor David Asman hosts an in-depth investigation into how we got here.

    ...

    )


    foo bar


    foo2 Saving Our Economy: What'$ Next? bar2


    the second print_r() gave nothing between the "foo bar". if using var_dump() it gives NULL
     
    winterheat, Apr 23, 2009 IP
  5. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes, but what IS $nsmedia - how do you implement it?
     
    szalinski, Apr 23, 2009 IP
  6. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok, if you need a sample, try

    $simpleXMLObj = simplexml_load_string(
    '<?xml version="1.0" encoding="UTF-8"?>
    <publisher:Item xmlns:publisher="http://www.whatever.com/">
      <entry>
        <name>How to clean everything</name>
        <price>$15.02</price>
        <publisher:name>Random House</publisher:name>
        <publisher:address>USA</publisher:address>
      </entry>
      <entry>
        <name><![CDATA[Paris Tour book]]></name>
        <price>$11.99</price> 
        <publisher:name><![CDATA[Lonely Planet]]></publisher:name>
        <publisher:address>France</publisher:address> 
      </entry>
    </publisher:Item>');
    
    print_r($simpleXMLObj->entry);
    print_r($simpleXMLObj->entry->name); 
    Code (markup):
    the output is:

    SimpleXMLElement Object
    (
        [name] => How to clean everything
        [price] => $15.02
    )
    SimpleXMLElement Object
    (
        [0] => How to clean everything
    )
    Code (markup):
     
    winterheat, Apr 23, 2009 IP
  7. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    now that we've established print_r seems to work, what happens when you try

    echo $simpleXMLObj->entry; should output 'Array'

    and

    echo $simpleXMLObj->entry['name']; should output 'How to clean everything'

    ??? :(
     
    szalinski, Apr 23, 2009 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    I don't get it - of course you get an array output if you use something "array'y" to display it - for instance print_r

    Why not just use "echo ($simpleXMLObj->entry->name);" and so forth?
     
    PoPSiCLe, Apr 23, 2009 IP
  9. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    that's what i'm trying to understand - why they have a fetish for print_r and var_dump... :rolleyes:
     
    szalinski, Apr 23, 2009 IP
  10. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    NO, my real question is,

    why

    print_r($simpleXMLObj->entry);

    prints out an object with a instance variable "name" as a string, and then when i print out that instance variable, it is a SimpleXMLElement object.
     
    winterheat, Apr 23, 2009 IP
  11. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    simple answer: because you're print_r'ing the object with it.
     
    szalinski, Apr 23, 2009 IP
  12. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    with other objects, if you print_r($obj) and you see an instance variable "name" that is a string, then you print_r($obj->name) and it will be a string.

    in the case above, it is NOT. it is an object. WHY?
     
    winterheat, Apr 23, 2009 IP
  13. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #13
    looking at the documentation, http://ie.php.net/manual/en/function.simplexml-load-string.php.

    obviously, it's because entry has some members and they become arrays.

    and the reason it's an object is because that's what simplexml_load_string does..

    and if that's your question, have you already solved how to echo out strings? coz i'd like to know how you do it, as there's more than one 'entry' group in the object.
     
    szalinski, Apr 23, 2009 IP
  14. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #14
    Okay, lets try this very slowly - you're not printing objects - you are printing arrays with the keys and values attached, ok?

    print_r($simpleXMLObj->entry); prints the array of the Entry-part, which, naturally, contains the [name] and [price] strings.
    When you then
    print_r($simpleXMLObj->entry->name); it PRINTS OUT THE ARRAY OF THE "name"-part - which will be SimpleXMLElement Object ( [0] => How to clean everything ) <- an array.

    Ok?

    If you simply PRINT or ECHO the $simpleXMLObj->entry->name value, you will get the string stored in "name" as a value, and it will show. What is confusing?
     
    PoPSiCLe, Apr 23, 2009 IP
  15. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    so SimpleXMLElement is an array but not an object? is that so?
     
    winterheat, Apr 23, 2009 IP
  16. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    for example,

    $obj = new stdClass();
    $obj->name = "hello world";
    $obj->description = "this is a string of something";
    
    print_r($obj);
    print_r($obj->name); 
    Code (markup):
    the result is

    stdClass Object
    (
        [name] => hello world
        [description] => this is a string of something
    )
    hello world
    Code (markup):
    you can see that the second print_r didn't give an object, unlike the SimpleXMLElement case we see above.
     
    winterheat, Apr 23, 2009 IP
  17. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #17
    Yes. Do you see any nested arrays in the latest code you posted? No? Exactly.
     
    PoPSiCLe, Apr 23, 2009 IP
  18. winterheat

    winterheat Peon

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    i don't see any "nested array"

    but i also don't see any nested array here too:

    SimpleXMLElement Object
    (
        [name] => How to clean everything
        [price] => $15.02
    )
    SimpleXMLElement Object
    (
        [0] => How to clean everything
    )
    Code (markup):
     
    winterheat, Apr 23, 2009 IP