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
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>

 

:-[

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.