Jump to content

Loop through nested XML nodes


GalaxyTramp

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/227621-loop-through-nested-xml-nodes/
Share on other sites

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>

 

:-[

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.