Need Help Parsing XML File
Posted: Fri Mar 27, 2009 10:12 am
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:
Many thanks in advance for help!
Mike Needham
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
?>Mike Needham