由于您已经拥有所需流程的流程图,因此应该非常简单。
我没有进行任何数据清理/转义,因为您可以根据自己的要求进行操作。
XML FILE: (save it as testFeed.xml)
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>1</id>
<name>book</name>
</product>
<product>
<id>2</id>
<name>pen</name>
</product>
</products>
PHP Code:
<?php
$xml = simplexml_load_file( 'testFeed.xml' );
$xpath = $xml->xpath( '//product' );
$feed = array();
foreach( $xpath as $node ) {
$feed[(string)$node->id] = (string)$node->name; // id is key, name is value
}
// delete all entries from table where the table entries are not in the xml
$sql = 'DELETE FROM `table` WHERE id NOT IN
(' . implode( ',', array_keys( $feed ) ) . ')';
$run = mysql_query( $sql, $link );
//Now, insert/update - USE MySQL's "INSERT ... ON DUPLICATE UPDATE" feature
foreach( $xpath as $key => $node ) {
$sql = 'INSERT INTO `table` (id,name) VALUES ("'.$key.'","'.$node.'")
ON DUPLICATE KEY UPDATE name="'.$node.'";'
$run = mysql_query( $sql, $link );
}
?>
MySQL INSERT ... ON DUPLICATE UPDATE示例在这里。
希望这可以帮助。