我需要加載大量的XML源並將信息保存到數據庫。我無法控制XML字段,但它們通常包含唯一的ID,標題,貨幣,價格和持續時間。數據庫中的XML標記
代碼下面僅以1個飼料做工精細:
function process_xml_file($path, $adv_id)
{
$xml = simplexml_load_file($path, 'SimpleXMLElement', LIBXML_NOCDATA);
$data = array();
$finished = array();
$counter = 0;
// loop through all items
foreach($xml->product as $product)
{
$product_id = strip_tags((string)$product->productID);
if(!in_array($product_id, $finished))
{
$country = $product->xpath('./extra/field[@name="country"]');
$data[$counter]['country'] = strip_tags((string)$country[0]);
$data[$counter]['title'] = strip_tags((string)$product->name);
$data[$counter]['currency'] = strip_tags((string)$product->price['currency']);
$data[$counter]['price'] = strip_tags((string)$product->price);
$duration = $product->xpath('./extra/field[@name="duration"]');
$data[$counter]['duration'] = strip_tags((string)$duration[0]);
// add this product to the finished array, we want to exclude duplicates
$finished[$counter] = $product_id;
$counter++;
}
}
return $data; // the data will be saved to database in an other method
}
我想拯救之類的東西PROD_ID和XPath(」 ./額外/場[@名稱= 「國」 ]'),因此我可以使用eval()輕鬆檢索不同Feed字段的值。我知道eval()是邪惡的,並且可以提供更好的建議。我是唯一一個處理這種數據的人,所以eval()的危險可能比平常小一些。
檢索PRODUCT_ID和標題工作正常,問題是在使用國家和持續時間的XPath,eval()函數將拋出類似的錯誤:
Parse error: syntax error, unexpected '"xpath('./additional/field[@na' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\xampp\htdocs\project\feed.php(220) : eval()'d code on line 1
例子:
// simple xml object of all products
$children = $xml->children();
$country = $tag['country']; // $tag is from the db
// loop through all products
foreach($children as $product)
{
$id = strip_tags($product->$product_id);
$country = $product->$country;
eval("\$country2 = \$product->\"{$country}\";");
echo $country2;
}
我的數據庫表:
CREATE TABLE IF NOT EXISTS `tbl_feeds_xml_tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`feed_id` int(11) NOT NULL,
`country` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`product_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`currency` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`price` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`duration` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `feed_id` (`feed_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
來自表格的結果:
Array
(
[id] => 1
[feed_id] => 1
[country] => xpath('./additional/field[@name="country"]')
[product_id] => productID
[title] => name
[currency] => price['currency']
[price] => price
[duration] => xpath('./additional/field[@name="duration"]')
)
XML飼料例如:
<?xml version="1.0" encoding="iso-8859-1"?>
<products>
<product>
<productID>32934</productID>
<name>Cruise Antillen en Zuid-Amerika & strand Curaçao</name>
<price currency="EUR">1405.00</price>
<extra>
<field name="country">Panama</field>
<field name="duration">12</field>
</extra>
</product>
..
etc.
..
</products>
我的問題是:如何才能讓我對所有飼料此功能工作?請記住,在其他Feed中,prod_id或country標籤的命名完全不同。
我想不起來,已經爲此掙扎了好幾天,在這個論壇上找不到答案。
對eval()替代品的建議也歡迎!
請在你的答案中明確,因爲我是一個PHP新手。
即使是你想要什麼 – Soundz