burge124 Posted March 4, 2008 Share Posted March 4, 2008 hi can anyone point me in the right direction for a good tutorial for uploading xml data from a file into a database, thanks ian Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 first you want to open the file, then read the file then save the file into a variable then save it to the database. fopen fread Quote Link to comment Share on other sites More sharing options...
drisate Posted March 4, 2008 Share Posted March 4, 2008 Download this class and use it like this <?php include_once("class_path_parser.php"); include ("include/xml-fonc.php"); $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 parsed!<br>"; } $parser2 = new Path_parser(); $parser2->set_handler("/STHS-FarmTeam/Team/Name","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Abbre","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Morale","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Point","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/GP","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/W","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/L","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/T","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/OTW","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/OTL","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/SOW","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/SOL","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/GF","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/GA","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeGP","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeW","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeL","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeT","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeOTW","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeOTL","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeSOW","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeSOL","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeGF","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/HomeGA","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/PPAttemp","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/PPGoal","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/PKAttemp","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/PKGoalGA","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/PKGoalGF","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/ShotsFor","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/ShotsAga","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/ShotsBlock","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/ShotsPerPeriod1","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/ShotsPerPeriod2","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/ShotsPerPeriod3","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/ShotsPerPeriod4","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/GoalsPerPeriod1","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/GoalsPerPeriod2","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/GoalsPerPeriod3","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/GoalsPerPeriod4","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Shutouts","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Goals","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Assists","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Points","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Pim","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/Hits","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/FaceOffWonOF","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/FaceOffWonDF","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/FaceOffWonNT","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/FaceOffTotalOF","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/FaceOffTotalDF","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/FaceOffTotalNT","xml_to_db2"); $parser2->set_handler("/STHS-FarmTeam/Team/EmptyNetGoal","xml_to_db2"); if(!$parser2->parse_file("include/xml/LNHSP3-FarmTeam.xml")) { print("Error:".$parser2->get_error()."<br/>\n"); } echo "LNHSP3-FarmTeam.xml parsed!<br>"; ?> I included in attachement the 2 XML files i used in this exemple, the class and function page I hope thats gona help. http://versatilebb.com/XML_to_DB.rar I am gona delete it when you confirme you dowloaded it Quote Link to comment Share on other sites More sharing options...
burge124 Posted March 4, 2008 Author Share Posted March 4, 2008 hi, any other ways of completing this task? the xml layout i must use is... <?xml version="1.0" encoding="ISO-8859-1"?> <question> <chapter> *** ENTER CHAPTER NAME HERE *** </chapter> <questiontitle> *** ENTER SOURCE OF QUESTION/ANSWER *** </questiontitle> <answera> *** ENTER ANSWER A HERE *** </answera> <answerb> *** ENTER ANSWER B HERE *** </answerb> <answerc> *** ENTER ANSWER C HERE *** </answerc> <correctanswer> *** ENTER CORRECT ANSWER HERE ( A B C ) *** </correctanswer> </question> and the database layout to accept the xml is... table - question column names - QuestionTitle, AnswerA, AnswerB, AnswerC, CorrectAnswer thanks Quote Link to comment Share on other sites More sharing options...
drisate Posted March 4, 2008 Share Posted March 4, 2008 ... the one i just gave works ... why are you looking for an other way? In your case use $parser1->set_handler("/question/chapter","xml_to_db1"); [...] and modify xml_to_db1 for your stuff Quote Link to comment Share on other sites More sharing options...
burge124 Posted March 4, 2008 Author Share Posted March 4, 2008 hi ive changed it to... $parser1->set_handler("/question/chapter","xml_to_questiondb" do i have to specify which table i place information into??? if so is it questiondb/question/chapter??? Quote Link to comment Share on other sites More sharing options...
drisate Posted March 5, 2008 Share Posted March 5, 2008 Then you also need to change the function name in the xml-fonc.php page function xml_to_questiondb($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()); } } } And of course change for your info 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.