burge124 Posted February 21, 2008 Share Posted February 21, 2008 hi, im parsing from an xml file and just need to know how to call those elements taken from the xml file into variables so that i can play around with them later? //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 "chapter": break; case "questiontitle": break; case "answera": break; case "answerb": break; case "answerc": break; case "correctanswer": break; } } from that information i want to create.... $chapter... $qtitle.... $ansa.... $ansb... and so on thanks Link to comment https://forums.phpfreaks.com/topic/92362-creating-variables-from-a-parsed-xml-doc/ Share on other sites More sharing options...
drisate Posted February 22, 2008 Share Posted February 22, 2008 i use this XML class parser. Makes stuff very easy Ex of use: function xml_to_db1($name,$attribs,$content) { if ($content!=""){ $content = explode("<$name>", $content); $content = explode("</$name>", $content[1]); $content[0]=addslashes($content[0]); } if ($name=="Name"){ $enter = mysql_query("INSERT INTO coach (id, name) VALUE ('', '$content[0]')") or die (mysql_error()); }else{ if ($content!=""){ $coach_select = mysql_query("SELECT * FROM coach order by id desc limit 0,1") or die (mysql_error()); $coach = mysql_fetch_array($coach_select); $enter = mysql_query("UPDATE coach SET $name='$content[0]' WHERE id='$coach[id]'") or die (mysql_error()); } } } $parser1 = new Path_parser(); $parser1->set_handler("/STHS-Coaches/Coach/Name","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/Team","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/Country","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/Age","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/PH","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/DF","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/OF","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/PD","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/EX","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/LD","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/PO","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/Contrac","xml_to_db1"); $parser1->set_handler("/STHS-Coaches/Coach/Salary","xml_to_db1"); if(!$parser1->parse_file("include/xml/LNHSP3-Coaches.xml")) { print("Error:".$parser1->get_error()."<br/>\n"); }else{ echo "LNHSP3-Coaches.xml has been parsed!<br>"; } Link to comment https://forums.phpfreaks.com/topic/92362-creating-variables-from-a-parsed-xml-doc/#findComment-473264 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.