数据库中的XML标记

时间:2013-07-22 19:24:15

标签: php xml xpath simplexml eval

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

以下代码仅适用于1个Feed:

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()轻松检索不同feed字段的值。我知道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 Feed示例:

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

我的问题是:如何让此功能适用于所有Feed?请注意,在其他Feed中,prod_id或country标记的名称完全不同。

我无法弄清楚,已经挣扎了几天,在这个论坛上找不到答案。

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

请在答案中说清楚,因为我是php的新手。

1 个答案:

答案 0 :(得分:0)

为什么你甚至需要eval()?如果存储检索每个feed所需值的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;
    }
}