vijayshekar Posted September 15, 2009 Share Posted September 15, 2009 HI, I am trying this PHP XML Expat PARSER example. The Xml is as below : <?xml version="1.0"?> <news> <story> <headline> Godzilla Attacks LA! </headline> <description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description> <link>http://newsdomain.com</link> </story> <story> <headline> Bigfoot Spotted at M.I.T. Dining Area </headline> <description>The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing. </description> <link>http://newsdomain.com</link> </story> <story> <headline> London Angel Saves England </headline> <description>The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her. </description> <link>http://newsdomain.com</link> </story> <story> <headline> Six-eyed Man to be Wed to an Eight-armed Woman </headline> <description>Uhhhmmm... No comment really... just a little creepy to see them together... </description> <link>http://newsdomain.com</link> </story> <story> <headline> Ahmed's Birthday Extravaganza! </headline> <description>The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides. </description> <link>http://newsdomain.com</link> </story> </news> The Php program is as below : <?php //Initialize the XML parser $parser=xml_parser_create(); //Function to use at the start of an element function start($parser,$element_name,$element_attrs) { switch($element_name) { case "NOTE": echo "-- Note --<br />"; break; case "TO": echo "To: "; break; case "FROM": echo "From: "; break; case "HEADING": echo "Heading: "; break; case "BODY": echo "Message: "; } } //Function to use at the end of an element function stop($parser,$element_name) { echo "<br />"; } //Function to use when finding character data function char($parser,$data) { echo $data; } //Specify element handler xml_set_element_handler($parser,"start","stop"); //Specify data handler xml_set_character_data_handler($parser,"char"); //Open XML file $fp=fopen("test.xml","r"); //Read data while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } //Free the XML parser xml_parser_free($parser); ?> This program outputs all the tags in xml. But I want only the headline and link tags separately and get the link hyperlinked. Kindly help me to display each tag of the xml separately and also exclude the undesired tags in xml . Thanks Quote Link to comment Share on other sites More sharing options...
ILMV Posted September 15, 2009 Share Posted September 15, 2009 Do you have to use xml_parser_create? It might be easier with SimpleXML... http://uk2.php.net/simplexml Quote Link to comment Share on other sites More sharing options...
vijayshekar Posted September 15, 2009 Author Share Posted September 15, 2009 Thats also not working for my problem, Let me know the modification for the above code I have included. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 15, 2009 Share Posted September 15, 2009 case-sensitivity in element names? NOTE != note Quote Link to comment Share on other sites More sharing options...
vijayshekar Posted September 15, 2009 Author Share Posted September 15, 2009 I am getting out of topic replies, kindly post answer related to my query. Quote Link to comment Share on other sites More sharing options...
vijayshekar Posted September 15, 2009 Author Share Posted September 15, 2009 Anyone help please ? Its urgent Quote Link to comment Share on other sites More sharing options...
dzelenika Posted September 16, 2009 Share Posted September 16, 2009 OK The way You doing this is completely wrong. You do not have to use XML parser to read XML document. Use simpleXML instead. Example: $xml = simplexml_load_file('test.xml'); foreach ($xml->story as $story) { echo $story->headline, '<br />'; echo $story->link, '<br />'; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.