kbrice Posted September 22, 2008 Share Posted September 22, 2008 I was wondering if anyone out there knows the proper way to upload XML data to a database. I have the ability to parse the XML file & display the data to a web page. AND I know how to INSERT data to my database, but i don't know how to combine both scripts. So, does anyone have any idea how to do this. I've provided some examples below. XML example: <?xml version="1.0" encoding="ISO-8859-1"?> <hotel> <name>La Sommità</to> <location>Puglia</from> <country>Italy</heading> </hotel> SQL query to upload data. $sql= "INSERT INTO hotelrecords ('id','name','location','country') " . "VALUES ($idValue, $nameValue, $locationValue, $countryValue)"; mysql_query($sql, $conn) or die('Could not upload data; ' . mysql_error()); PHP parse to Database. ??? That's where I need help. Link to comment https://forums.phpfreaks.com/topic/125348-parsing-an-xml-file-using-php-upload-it-to-a-database/ Share on other sites More sharing options...
kbrice Posted September 22, 2008 Author Share Posted September 22, 2008 Fixed errors... <?xml version="1.0" encoding="ISO-8859-1"?> <hotel> <id>102</id> <name>La Sommità</name> <location>Puglia</location> <country>Italy</country> </hotel> Link to comment https://forums.phpfreaks.com/topic/125348-parsing-an-xml-file-using-php-upload-it-to-a-database/#findComment-647958 Share on other sites More sharing options...
kbrice Posted September 23, 2008 Author Share Posted September 23, 2008 Alright guys, this is the PHP parse code, It works great even with huge xml files. If there is a good spot to add the INSERT query, I think it would be within the endTag() function. Though one of the problem is that the query has to be written differently for each values I'm guessing. Hope this helps. <?php // Connect to the Database. //========================================== define('SQL_HOST','********'); // stars to be replaced with the correct info define('SQL_USER','********'); define('SQL_PASS','********'); define('SQL_DB','********'); $conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Could not connect to the database; ' . mysql_error()); mysql_select_db(SQL_DB, $conn) or die('Could not select database; ' . mysql_error()); // PARSE XML SETUP //========================================== $file = "filename.xml"; // function to handle the start tags function startTag($parser, $data){ echo "<b>"; } // function to handle the data between the tags function contents($parser, $data){ echo $data; } // function to handle the end tags function endTag($parser, $data){ echo "</b><br />"; } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startTag", "endTag"); xml_set_character_data_handler($xml_parser, "contents"); $fp = fopen($file, "r"); $data = fread($fp, filesize($file)); if(!(xml_parse($xml_parser, $data))){ die("Error on line " . xml_get_current_line_number($xml_parser)); } xml_parser_free($xml_parser); fclose($fp); mysql_close($conn); ?> Link to comment https://forums.phpfreaks.com/topic/125348-parsing-an-xml-file-using-php-upload-it-to-a-database/#findComment-648696 Share on other sites More sharing options...
nadeemshafi9 Posted September 23, 2008 Share Posted September 23, 2008 i realy dont know what your trying to achieve so its hard to help maybe if you tell us what your trying to do rathe rthan use words like parse too much Link to comment https://forums.phpfreaks.com/topic/125348-parsing-an-xml-file-using-php-upload-it-to-a-database/#findComment-648738 Share on other sites More sharing options...
nadeemshafi9 Posted September 23, 2008 Share Posted September 23, 2008 why dont you write a function to read the xml file and insert it into the databse you may ask arround if a text file is best or a blob im not sure on that for xml buy seing as its xml text feild should do teh job Link to comment https://forums.phpfreaks.com/topic/125348-parsing-an-xml-file-using-php-upload-it-to-a-database/#findComment-648740 Share on other sites More sharing options...
kbrice Posted September 23, 2008 Author Share Posted September 23, 2008 In my previous post I have the php parse to output the data to a page, but I don't know how to setup the code to upload it to a database. Idk how else to explain it better, I don't want to confuse anyone. Link to comment https://forums.phpfreaks.com/topic/125348-parsing-an-xml-file-using-php-upload-it-to-a-database/#findComment-648751 Share on other sites More sharing options...
nadeemshafi9 Posted September 23, 2008 Share Posted September 23, 2008 <?php $data = fread($fp, filesize($file)); // you must add this stuff after this line in ur code and fill in teh db connection stuff //$data is teh data so do an INSERT to ur database with $data $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $query = "INSERT INTO xml_table (id, xml) VALUES (NULL, '".$data."')"; // the table should be 2 feilds 1 int called id and 1 called xml of type text field auto increment id and make it primary key $mysql_query($query); mysql_close($link); ?> Link to comment https://forums.phpfreaks.com/topic/125348-parsing-an-xml-file-using-php-upload-it-to-a-database/#findComment-648795 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.