...

View Full Version : Help using simplexml



michaelespinosa
07-24-2008, 05:38 PM
I am using simplexml to load a file:



$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:


$title = $xml->title

This doesn't seem to work for me. Am I doing something wrong. Thanks for the help.

oesxyl
07-24-2008, 05:50 PM
- 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

michaelespinosa
07-24-2008, 06:27 PM
I made sure $xml is not false and there is a title element. the path is:


$xml->entry->title

Are you allowed to chain like that? Because it's not working for me when I do:


print "<h3>$xml->entry->title</h3>";

oesxyl
07-24-2008, 06:50 PM
there are multiple entry so you must say which one you want:


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

oesxyl
07-24-2008, 06:55 PM
I test what I post and fail, try this way:


print "<h3>".$xml->entry[0]->title."</h3>";

this work.

regards

michaelespinosa
07-24-2008, 10:57 PM
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 (http://www.rockchurchfamily.com/test/rss.php).


$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
"<h3>".$xml->entry[0]->title."</h3>"; to get it to work and mine
print "<h3>$xml->id</h3>"; worked for this one.?

oesxyl
07-24-2008, 11:09 PM
did you read previous posts?

try this:

$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

oesxyl
07-24-2008, 11:14 PM
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
"<h3>".$xml->entry[0]->title."</h3>"; to get it to work and mine
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

michaelespinosa
07-24-2008, 11:23 PM
oesxyl: Thanks, that what I thought it might be.

michaelespinosa
07-29-2008, 12:53 AM
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.


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 )
)
)

oesxyl
07-29-2008, 01:19 AM
try this:


print $xml->entry->link[1]['title'];


because 'title' is key in aray link[1]


[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

michaelespinosa
07-29-2008, 06:03 AM
Thanks oesxyl, that worked. I am having trouble finding examples and documentation on this. Do you have any links about this. Thanks again.

oesxyl
07-29-2008, 01:21 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum