Xml processing...traversing the xml document

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
deostroll
Forum Newbie
Posts: 7
Joined: Sat Dec 12, 2009 8:10 am

Xml processing...traversing the xml document

Post by deostroll »

I am trying to traverse an xml document with the following php code:

Code: Select all

 
<?php
$r ="http://twitter.com/statuses/user_timeline/deostroll.xml";
 
$res = file_get_contents( $r );
$d = new DOMDocument();
 
$d->loadXML($res);
if ($d == null)
{
    echo('document is null');
}
$e = $d->documentElement;
if ($e == null)
{
    echo('element is null');
}
 
foreach ( $e->childNodes as $status )
{
    #$created_at = $status->getElementsByTagName('created_at')->item(0);
    echo print_r($status);
}
?>
 
The xml document has a structure like follows: (it is similar to your twitter user timelines) root element is called statuses; there are lot of child elements under this called status; there are again child nodes to status with data pertaining to status.

In the above code if you see the commented line I get an error while trying to execute $status->getElementsByTagName(). Later I find out $status aint of type DOMNode. Its DOMText. I need to find an element under each status node, and print data which comes inside of it...something like:

Code: Select all

<status>
<created_at>10/12/2007 14:53:25</created_at><!--trrying to print value inside this tag -->
.
.
.
</status>
Post Reply