mojomystery Posted January 18, 2013 Share Posted January 18, 2013 (edited) First, thanks in advance for any help. Here's my issue. I've read through tons of similar problems here but I don't think I'm understanding this enough to make this work. I have an XML file such as: <unitlist> <unit> <iterator>0</iterator> <route>2</route> <rt_name>'Riverfront Streetcar'</rt_name> <name>457</name> <lat>2956.21014</lat> <lng>-9004.62839</lng> <bearing>2.14501</bearing> <car_type>'RTA Street Cars'</car_type> <speed>16.57122</speed> <GMT>043432</GMT>[/indent] </unit> <unit> <iterator>1</iterator> <route>10</route> <rt_name>'Tchoupitoulas'</rt_name> <name>250</name> <lat>2958.0927</lat> <lng>-9005.28724</lng> <bearing>1.532399</bearing> <car_type>'RTA Buses'</car_type> <speed>0.6904677</speed> <GMT>104620</GMT>[/indent] </unit> </unitlist> What I want to do is, for each <unit>, import all attributes of that unit (iterator,route,rt_name, etc, etc, etc) into my mySQL database. My problem is that, all it's doing is importing the same attributes of the first <unit> the number of times that <unit> exists. In this example, it's importing the same information from the first <unit> twice, as two records/rows. If I had 90 <unit>s, it would import 90 identical records. Here's my code: foreach($unitlist->unit as $unit) { //insert into database mysql_query("INSERT INTO businfo (iterator,route,rt_name,name,lat,lng,bearing,car_type,speed,GMT)VALUES (\"$iterator\",\"$route\", \"$rt_name\", \"$name\", \"$lat\", \"$lng\", \"$bearing\", \"$car_type\", \"$speed\", \"$GMT\")") or die(mysql_error()); } Can anyone explain what I'm doing wrong? Thanks again for the help! - dan - Edited January 18, 2013 by mojomystery Quote Link to comment https://forums.phpfreaks.com/topic/273301-simplexml-to-mysql/ Share on other sites More sharing options...
requinix Posted January 18, 2013 Share Posted January 18, 2013 You have to get the values of $iterator and such with $unit->iterator, not $unitlist->unit->iterator. Quote Link to comment https://forums.phpfreaks.com/topic/273301-simplexml-to-mysql/#findComment-1406626 Share on other sites More sharing options...
Barand Posted January 18, 2013 Share Posted January 18, 2013 try <?php $xml = simplexml_load_file('units.xml'); $data = $fields = array(); foreach ($xml->xpath('unit') as $unit) { $fields = array_keys((array)($unit)); $data[] = '(' . join(', ', (array)$unit) . ')'; } $sql = "INSERT INTO businfo (" . join(', ', $fields) . ") VALUES\n" ; $sql .= join (",\n", $data); echo "<pre>$sql</pre>"; ?> RESULT INSERT INTO businfo (iterator, route, rt_name, name, lat, lng, bearing, car_type, speed, GMT) VALUES (0, 2, 'Riverfront Streetcar', 457, 2956.21014, -9004.62839, 2.14501, 'RTA Street Cars', 16.57122, 043432), (1, 10, 'Tchoupitoulas', 250, 2958.0927, -9005.28724, 1.532399, 'RTA Buses', 0.6904677, 104620) Quote Link to comment https://forums.phpfreaks.com/topic/273301-simplexml-to-mysql/#findComment-1406753 Share on other sites More sharing options...
mojomystery Posted January 21, 2013 Author Share Posted January 21, 2013 (edited) @Barand, Thank you very much. I think this produces exactly what I need. In my first test, however, I noticed that this company's XML file is prone to bad formatting which breaks the query. For example, their latest version contains this: <unit> <iterator>35</iterator> <route>80</route> <rt_name>'Louisa'</rt_name> <name>173</name> <lat>2900</lat> <lng>-9000</lng> <bearing>-1</bearing> <car_type>'RTA Buses'</car_type> <speed>-1</speed> <GMT/>[/size][/font][/color] </unit> Notice how they are missing a <GMT>? This breaks the query and it won't execute when it hits that line. Is there any way to account for mistakes like this so it doesn't break? Thanks again for your help and sorry for the delay in responding. Today is the first day I've been able to get back to working on this. - dan - Edited January 21, 2013 by mojomystery Quote Link to comment https://forums.phpfreaks.com/topic/273301-simplexml-to-mysql/#findComment-1407320 Share on other sites More sharing options...
Barand Posted January 21, 2013 Share Posted January 21, 2013 (edited) The formatting is correct where there is no value for an element <GMT /> is equivalent to <GMT></GMT> You could do something like this <?php $xml = simplexml_load_file('units.xml'); $data = $fields = array(); foreach ($xml->xpath('unit') as $unit) { $fields = array_keys((array)($unit)); if ($unit->GMT == '') $unit->GMT = 0; // check for no value $data[] = '(' . join(', ', (array)$unit) . ')'; } $sql = "INSERT INTO businfo (" . join(', ', $fields) . ") VALUES\n" ; $sql .= join (",\n", $data); echo "<pre>$sql</pre>"; ?> Edited January 21, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273301-simplexml-to-mysql/#findComment-1407323 Share on other sites More sharing options...
mojomystery Posted January 21, 2013 Author Share Posted January 21, 2013 Thanks again! - dan - Quote Link to comment https://forums.phpfreaks.com/topic/273301-simplexml-to-mysql/#findComment-1407326 Share on other sites More sharing options...
beartooth Posted January 14, 2014 Share Posted January 14, 2014 On a related note, what about a scenario where you have sub-elements that you want to capture and add into the data array as well? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/273301-simplexml-to-mysql/#findComment-1465147 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.