Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Question Help using simplexml

    I am using simplexml to load a file:

    PHP Code:
    $xml simplexml_load_file('http://www.blogger.com/feeds/6386500013791780626/posts/default?max-results=1'); 
    I want to access the certian elements of the xml. For example if I wanted to assign the value of the title element to a title variable:

    PHP Code:
    $title $xml->title 
    This doesn't seem to work for me. Am I doing something wrong. Thanks for the help.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    - check if $xml is not false
    - check if you have a title node in the document

    the simplest way to find xml structure is to print_r or var_dump different part of $xml.

    regards

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    I made sure $xml is not false and there is a title element. the path is:

    PHP Code:
    $xml->entry->title 
    Are you allowed to chain like that? Because it's not working for me when I do:

    PHP Code:
    print "<h3>$xml->entry->title</h3>"
    Last edited by michaelespinosa; 07-24-2008 at 07:45 PM.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    there are multiple entry so you must say which one you want:

    PHP Code:
    print "<h3>$xml->entry[0]->title</h3>"
    a link with some examples, if you don't already have:

    http://www.php.net/manual/en/simplexml.examples.php

    regards

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    I test what I post and fail, try this way:

    PHP Code:
    print "<h3>".$xml->entry[0]->title."</h3>"
    this work.

    regards
    Last edited by oesxyl; 07-25-2008 at 12:15 AM.

  • Users who have thanked oesxyl for this post:

    michaelespinosa (07-25-2008)

  • #6
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    I changed my code up to use SimpleXMLElement instead of simplexml_load_file. I am able to access the "id" node but not the "title" or "author->name". I just can't understand how to access the nodes I wan't. Check out what I have and then you can see the result here.

    PHP Code:
    $source = ('http://www.blogger.com/feeds/6386500013791780626/posts/default?max-results=1');

    $xmlstr file_get_contents($source);

    $xml = new SimpleXMLElement($xmlstr);



    print 
    "<h3>$xml->id</h3>";
    print 
    "<p>$xml->entry->content</p>";
    print 
    "<h2>$xml->author->name</h2>";

    print_r ($xml); 
    oesxyl: Thanks for your last post, that worked well. I am trying it another way. Also why did you have to put quotes around the html elements print
    PHP Code:
    "<h3>".$xml->entry[0]->title."</h3>"
    to get it to work and mine
    PHP Code:
    print "<h3>$xml->id</h3>"
    worked for this one.?
    Last edited by michaelespinosa; 07-25-2008 at 12:07 AM. Reason: update to oesxyl

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    did you read previous posts?

    try this:
    PHP Code:
    $xml simplexml_load_file('http://www.blogger.com/feeds/6386500013791780626/posts/default?max-results=1');

    print 
    "<h3>".$xml->id "</h3>";
    print 
    "<p>" $xml->entry[0]->content "</p>";
    print 
    "<h2>" $xml->author->name "</h2>"
    regards

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by michaelespinosa View Post
    oesxyl: Thanks for your last post, that worked well. I am trying it another way. Also why did you have to put quotes around the html elements print
    PHP Code:
    "<h3>".$xml->entry[0]->title."</h3>"
    to get it to work and mine
    PHP Code:
    print "<h3>$xml->id</h3>"
    worked for this one.?
    php don't know where the variable is ended. I make this mistake in #4,

    regards

  • #9
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    oesxyl: Thanks, that what I thought it might be.
    Last edited by michaelespinosa; 07-25-2008 at 12:24 AM. Reason: reply

  • #10
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Ok, I have been playing with this for a while and having trouble again. I need access the entry->link[1]->title. I can't seem to do it though. I know that I need to specify link[1] but I don't know how to also specify link['title']. How do I do this?

    I tried to tidy up what print_r gives me from the xml thats imported below and also add "<<<<<<<<<" to what node I am trying to access. Thanks.

    PHP Code:
    SimpleXMLElement Object 

    [
    id] => tag:blogger.com,1999:blog-6386500013791780626 
    [updated] => 2008-07-28T14:07:56.593-07:00 
    [title] => bloggy 

        
    [link] => Array ( 
           [
    0] => SimpleXMLElement Object 
           [@
    attributes] => Array ( 
                [
    rel] => alternate 
                
    [type] => text/html 
                
    [href] => http://therockyouth.blogspot.com/ ) ) 
           
    [1] => SimpleXMLElement Object 
           [@
    attributes] => Array ( 
                [
    rel] => http://schemas.google.com/g/2005#feed 
                
    [type] => application/atom+xml 
                
    [href] => http://therockyouth.blogspot.com/feeds/posts/default ) ) 
           
    [2] => SimpleXMLElement Object 
           [@
    attributes] => Array ( 
                [
    rel] => self 
                
    [type] => application/atom+xml 
                
    [href] => http://www.blogger.com/feeds/6386500013791780626/posts/default?max-results=1 ) ) 
                

                
    [
    author] => SimpleXMLElement Object 
        [
    name] => therockyouth 
        
    [uri] => http://www.blogger.com/profile/07825922733658458595 
        
    [email] => noreply@blogger.com 
        
    [
    generator] => Blogger 

        
    [entry] => SimpleXMLElement Object 
            [
    id] => tag:blogger.com,1999:blog-6386500013791780626.post-9111396441055026164 
            
    [published] => 2008-07-25T21:41:00.000-07:00 
            
    [updated] => 2008-07-28T14:07:56.605-07:00 
            
            
    [category] => Array ( 
                [
    0] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    scheme] => http://www.blogger.com/atom/ns# 
                    
    [term] => youth ) ) 
                [
    1] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    scheme] => http://www.blogger.com/atom/ns# 
                    
    [term] => mortal kombat ) ) 
                [
    2] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    scheme] => http://www.blogger.com/atom/ns# 
                    
    [term] => blog ) ) 
                ) 
            [
    title] => It Has Begun
            [
    content] => Content goes here!

            [
    link] => Array ( 
                [
    0] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    rel] => alternate 
                    
    [type] => text/html 
                    
    [href] => http://therockyouth.blogspot.com/2008/07/for-more-bloggings-from-therockyouth.html 
                    
    [title] => It Has Begun! ) ) 
                [
    1] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    rel] => replies 
                    
    [type] => text/html 
                    
    [href] => http://www.blogger.com/comment.g?blogID=6386500013791780626&postID=9111396441055026164 
                 
                                    
    [title] => 0 Comments ) ) <<<<<<<<<<<<<<

                [
    2] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    rel] => replies 
                    
    [type] => application/atom+xml 
                    
    [href] => http://therockyouth.blogspot.com/feeds/9111396441055026164/comments/default 
                    
    [title] => Post Comments ) ) 
                [
    3] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    rel] => self 
                    
    [type] => application/atom+xml 
                    
    [href] => http://www.blogger.com/feeds/6386500013791780626/posts/default/9111396441055026164 ) ) 
                
    [4] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    rel] => edit 
                    
    [type] => application/atom+xml 
                    
    [href] => http://www.blogger.com/feeds/6386500013791780626/posts/default/9111396441055026164 ) ) 
            

            [
    author] => SimpleXMLElement Object 
                [
    name] => therockyouth 
                
    [uri] => http://www.blogger.com/profile/07825922733658458595 
                
    [email] => noreply@blogger.com 
        ) 


  • #11
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    try this:
    PHP Code:
    print $xml->entry->link[1]['title']; 
    because 'title' is key in aray link[1]
    PHP Code:
    [link] => Array ( 
                [
    1] => SimpleXMLElement Object 
                [@
    attributes] => Array ( 
                    [
    rel] => replies 
                    
    [type] => text/html 
                    
    [href] => http://www.blogger.com/comment.g?blogID=6386500013791780626&postID=9111396441055026164 
                 
                                    
    [title] => 0 Comments ) ) <<<<<<<<<<<<<< 
    regards

  • Users who have thanked oesxyl for this post:

    michaelespinosa (07-29-2008)

  • #12
    New Coder
    Join Date
    Aug 2007
    Posts
    69
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Thanks oesxyl, that worked. I am having trouble finding examples and documentation on this. Do you have any links about this. Thanks again.

  • #13
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by michaelespinosa View Post
    Thanks oesxyl, that worked. I am having trouble finding examples and documentation on this. Do you have any links about this. Thanks again.
    this is what I use for simplexml( documentation):
    http://www.php.net/manual/en/book.simplexml.php

    for examples, I post in one of my previous thread, this:
    http://www.php.net/manual/en/simplexml.examples.php

    I don't really use simplexml, I use DOM instead, therefor I don't know if there is some better documentation/examples then that,

    regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •