0

我需要加载大量 XML 提要并将信息保存到数据库中。我无法控制 XML 字段,但它们通常包含唯一 ID、标题、货币、价格和持续时间。

以下代码仅适用于 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('./extra/field[@name="country"]') 之类的内容保存在数据库中,这样我就可以使用 eval() 轻松检索不同提要字段的值。我知道 eval() 是邪恶的,并且愿意接受更好的建议。我是唯一一个处理这种数据的人,所以 eval() 的危险可能比平时要小一些。

检索 product_id 和 title 工作正常,问题出在国家和使用 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 &amp; 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>

我的问题是:如何使此功能适用于所有提要?请记住,在其他提要中,prod_id 或国家/地区标签的名称完全不同。

我想不通,已经为此苦苦挣扎了好几天,在这个论坛上找不到答案。

也欢迎对 eval() 替代方案的建议!

请在你的答案中明确,因为我是 php 的新手。

4

1 回答 1

0

为什么你甚至需要 eval()?如果您存储 xpath 表达式来检索每个提要所需的值,您只需要检索该表达式并将其传递给 DOM 系统,例如

数据库:

feeds (id, url)
feed_xpath (id, feed_id, value_being_fetched, xpath_expression)

提取特定的提要,例如http://example.com/feed.xml,然后提取其相关的 xpath 内容:

$dom = new DOMDocument();
$dom->loadHTML($feed_url);
$xpath = new DOMXPath($dom);

$values = array();
foreach($feeds as $feed) { // $feeds being selected from the feeds_xpath table
    $nodelist = $xpath->query($feed['xpath'])
    foreach($nodelist as $node) {
        $array[$feed['value_being_fetched']] = $node->nodeValue;
    }
}
于 2013-07-22T19:37:27.940 回答