I'm attempting to cherry pick elements from a large xml file (inventory for a boat dealer with all the specs for each unit) and push that data into two separate tables. I don't know if I need two Xpaths to do this or if it can be compressed into one.
The XML file holds about 50+ elements with child elements per boat but I wanted to take this one step at a time until I get the hang of doing this and left it at three, as an example, before adding complexities.
I used this previous answer to model the script so far and added what I needed:
<?php $db = new mysqli('localhost', 'username', 'password', 'database'); $boats=simplexml_load_file("WinboatsWebXMLAllRevA.xml") or die ("Error: Cannot create object"); $values = <<<XPATH ( |element |element |element ) XPATH; $pattern_custom = <<<SQL INSERT INTO pmb_rsdirectory_entries_custom ( column1, column2, column3 ) VALUES ( '%s', '%s', '%s' ) SQL; foreach ($boats as $boat) { $data = $boat->xpath($values); $escaped = array_map('mysql_real_escape_string', $data); $query = vsprintf($pattern, $escaped); $result - mysql_query($query); if(mysql_errno()) { printf( '<h4 style="color: red;">Query Error:</h4> <p>(%s) - %s</p> <p>Query: <pre>%s</pre> </p> </hr />', mysql_errno(), htmlspecialchars(mysql_errno()), htmlspecialchars($query) ); } } $values_custom = <<<XPATH ( |element3 |element4 |element5 ) XPATH; $pattern_custom = <<<SQL INSERT INTO pmb_rsdirectory_entries ( column3, column4, column5 ) VALUES ( '%s', '%s', '%s' ) SQL; foreach ($boats as $boat) { $data = $boat->xpath($values_custom); $escaped = array_map('mysql_real_escape_string', $data); $query = vsprintf($pattern_custom, $escaped); $result - mysql_query($query); if(mysql_errno()) { printf( '<h4 style="color: red;">Query Error:</h4> <p>(%s) - %s</p> <p>Query: <pre>%s</pre> </p> </hr />', mysql_errno(), htmlspecialchars(mysql_errno()), htmlspecialchars($query) ); } } ?> If there is a better way to manage this I am certainly open to suggestions.
No comments:
Post a Comment