GalaxyTramp Posted February 14, 2011 Share Posted February 14, 2011 Hi I have searched around for a solution to this and either fail to grasp how to get it to work or just cannot find the correct solution. I have the following XML file: <?xml version="1.0" encoding="UTF-8"?> <root> <product> <id>153</id> <images> <image id="1"> <url>http://www.mysite.com/product/images/1322.jpg</url> <title> <en>Book 1</en> </title> </image> <image id="2"> <url>http://www.mysite.com/product/images/1321.jpg</url> <title> <en>Book 2</en> </title> </image> <image id="3"> <url>http://www.mysite.com/product/images/1316.jpg</url> <title> <en>Book 3</en> </title> </images> </product> <product> <id>154</id> <images> <image id="1"> <url>http://www.mysite.com/product/images/1322.jpg</url> <title> <en>Book 1</en> </title> </image> <image id="2"> <url>http://www.mysite.com/product/images/1321.jpg</url> <title> <en>Book 2</en> </title> </image> <image id="3"> <url>http://www.mysite.com/product/images/1316.jpg</url> <title> <en>Book 3</en> </title> </images> </product> </root> $xml = 'example.xml'; // URL for feed. try{ $feed = new SimpleXMLElement($xml, null, true); }catch(Exception $e){ echo $e->getMessage(); exit; } $sql = 'INSERT INTO images (`id`, `url`) VALUES '; foreach($feed->property as $property){ $sql .= sprintf( "\n('%d', '%s'),", $property->id, mysql_real_escape_string($property->images->image->url) ); } $sql = rtrim($sql, ',') . ';'; What I need to do is loop through each of the image url nodes and insert them into my DB with the relevant id. Can somebody point me in the right direction with this please its driving me nuts! Regards GT Quote Link to comment Share on other sites More sharing options...
GalaxyTramp Posted February 14, 2011 Author Share Posted February 14, 2011 Whoops! published the wrong XML SORRY Should read: <?xml version="1.0" encoding="UTF-8"?> <root> <property> <id>153</id> <images> <image id="1"> <url>http://www.mysite.com/product/images/1322.jpg</url> <title> <en>Title 1</en> </title> </image> <image id="2"> <url>http://www.mysite.com/product/images/1321.jpg</url> <title> <en>Title 2</en> </title> </image> <image id="3"> <url>http://www.mysite.com/product/images/1316.jpg</url> <title> <en>Title 3</en> </title> </images> </property> <property> <id>154</id> <images> <image id="1"> <url>http://www.mysite.com/product/images/1322.jpg</url> <title> <en>Title 1</en> </title> </image> <image id="2"> <url>http://www.mysite.com/product/images/1321.jpg</url> <title> <en>Title 2</en> </title> </image> <image id="3"> <url>http://www.mysite.com/product/images/1316.jpg</url> <title> <en>Title 3</en> </title> </images> </property> </root> 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.