Need Help Parsing XML File

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
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Need Help Parsing XML File

Post by irishmike2004 »

Greetings:

It has been a very long time since I tried to parse XML with PHP! I can't seem to remember how to do it exactly.

I have been looking at tutorials on-line on how to do it and I must have done it differently because none of them are making sense to me.

The XML file I am trying to parse is my twitter account (latest tweet). The URL is:
http://twitter.com/statuses/user_timeli ... ml?count=1

This gives you the XML file I am trying to parse. I can of course get that read in using fopen.

The ONLY tags I am interested in for my site are the <text></text> and the <created_at><created_at> and I want to put the stuff in those tags into two variables which I will eventually display on my page.

What would be the best way to parse for JUST those two tags given the XML file above? Please advise and give code examples for that part.

The code I am using so far is:

Code: Select all

<?php
 
//test parser code
 
$xml_parser = xml_parser_create(); //creates an XML parser
 
//set functions to handle opening and closing tags
xml_set_element_handler($xml_parser, "startElement", "endElement"); 
 
//set function to handle blocks of character data
xml_set_character_data_handler($xml_parser, "characterData");
 
//open XML file for reading
$fp = fopen("http://twitter.com/statuses/user_timeline/iainnitro.xml?count=1", "r")
    or die("Error:  Could Not read XML file.");
 
//read XML file
while($data = fread($fp, 4096))
{
    xml_parse($xml_parser, $data, feof($fp)) 
        or die (sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)),
        xml_get_current_line_number($xml_parser)));
    
}
 
//close XML File
fclose($fp);
 
//free the parser
xml_parser_free($xml_parser);
 
//process data from XML
 
 
 
 
 
 
?>
Many thanks in advance for help!

Mike Needham
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Re: Need Help Parsing XML File

Post by irishmike2004 »

Found my old code for doing this. Thanks to anyone that was looking at it to help me, but I have it solved now.

Have a great day!
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Need Help Parsing XML File

Post by atonalpanic »

User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Need Help Parsing XML File

Post by php_east »

irishmike2004 wrote:Found my old code for doing this. Thanks to anyone that was looking at it to help me, but I have it solved now.
Have a great day!
would you care to share ? i have the exact same requirement, and will be looking at it right now.
will post my code if it's any good.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Need Help Parsing XML File

Post by php_east »

here it is anyway...

Code: Select all

$doc2 = new DOMDocument();
$doc2->loadHTML($html);
$a  = $doc2->getElementsByTagName( "text" )->item(0); 
$b  = $doc2->getElementsByTagName( "created_at" )->item(0);  
echo $a->nodeValue; 
echo $b->nodeValue; 
Post Reply