jgd Posted November 2, 2007 Share Posted November 2, 2007 Maybe I am being ridiculously stupid here but for the life of me I cannot figure out what is wrong with my code here, at the end of this parser I am trying to echo a certain xml element depending on a string, so that I can pass it on from a form, any chance any of you guys can spot a problem? Code: <?php if( ! ($fp = fopen( "./sponsor1.xml" , "r" )) ) die("Couldn't open xml file!"); $person_counter = 0; $person_data = array(); $xml_current_tag_state = ''; function startElementHandler( $parser, $element_name, $element_attribs ) { global $person_counter; global $person_data; global $xml_current_tag_state; if( $element_name == "STORY" ) { $person_data[$person_counter]["author"] = $element_attribs["AUTHOR"]; } else { $xml_current_tag_state = $element_name; } } function endElementHandler( $parser, $element_name ) { global $person_counter; global $person_data; global $xml_current_tag_state; $xml_current_tag_state = ''; if( $element_name == "STORY" ) { $person_counter++; } } function characterDataHandler( $parser , $data ) { global $person_counter; global $person_data; global $xml_current_tag_state; if( $xml_current_tag_state == '' ) return; if( $xml_current_tag_state == "STORYTYPE" ) { $person_data[$person_counter]["storytype"] = $data; } if( $xml_current_tag_state == "HEADLINE" ) { $person_data[$person_counter]["headline"] = $data; } if( $xml_current_tag_state == "ABSTRACT" ) { $person_data[$person_counter]["abstract"] = $data; } if( $xml_current_tag_state == "BODYTEXT" ) { $person_data[$person_counter]["bodytext"] = $data; } } if( !($xml_parser = xml_parser_create()) ) die("Couldn't create XML parser!"); xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler"); xml_set_character_data_handler($xml_parser, "characterDataHandler"); while( $data = fread($fp, 4096) ) { if( !xml_parse($xml_parser, $data, feof($fp)) ) { break; // get out of while loop if we're done with the file } } xml_parser_free($xml_parser); echo $person_data[0]["headline"] ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> </head> <body> <?php // This is the line that defines what type of article is shown //i.e. the page needs to be fed the article string //Here it's just set as news (For Now) $article = "news"; for ( $i=0 ; $i < $person_counter ; ++$i ) // { $thisName = $person_data[$i]["storytype"]; if ($thisName == $article) { echo "<b>we have match at position number: ". $i . "</b><br>"; $thisPos = $i; } } echo $person_data[$thisPos]["headline"] ?> </body> </html> Any help Would be really appreciated, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/75819-solved-i-am-going-insane-reading-xml-problem/ Share on other sites More sharing options...
jgd Posted November 2, 2007 Author Share Posted November 2, 2007 Oh and I'm getting no error messages, just nothing else either.. PHP Version 5.2.3 Tried on a xampp localhost setting and two different hosting severs all to no avail Quote Link to comment https://forums.phpfreaks.com/topic/75819-solved-i-am-going-insane-reading-xml-problem/#findComment-383739 Share on other sites More sharing options...
revraz Posted November 2, 2007 Share Posted November 2, 2007 Just some things I saw: for ( $i=0 ; $i < $person_counter ; ++$i ) Shouldn't that be $i++ Quote Link to comment https://forums.phpfreaks.com/topic/75819-solved-i-am-going-insane-reading-xml-problem/#findComment-383752 Share on other sites More sharing options...
jgd Posted November 2, 2007 Author Share Posted November 2, 2007 oh yeah.. still doesn't work but well spotted and thanks! Quote Link to comment https://forums.phpfreaks.com/topic/75819-solved-i-am-going-insane-reading-xml-problem/#findComment-383754 Share on other sites More sharing options...
jgd Posted November 2, 2007 Author Share Posted November 2, 2007 The first echo works, echo $person_data[0]["headline"] it's just when i try to set the place number with a variable that it won't work echo $person_data[$thisPos]["headline"] Quote Link to comment https://forums.phpfreaks.com/topic/75819-solved-i-am-going-insane-reading-xml-problem/#findComment-383758 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.