Php 数据库中的XML标记

Php 数据库中的XML标记,php,xml,xpath,simplexml,eval,Php,Xml,Xpath,Simplexml,Eval,我需要加载大量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; // l

我需要加载大量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()的危险性可能会比平常小一些

检索产品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 &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>

32934
美国安的列斯邮轮公司;库拉索岛海滨酒店
1405
巴拿马
12
..
等
..
我的问题是:如何使此功能适用于所有提要?请记住,在其他提要中,产品id或国家/地区标记的名称完全不同

我想不出来,已经挣扎了好几天,在这个论坛上找不到答案

也欢迎对eval()备选方案提出建议

由于我是php的新手,请在回答中明确说明。

为什么您甚至需要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;
    }
}

尽管有一大堆的文本,我还是不明白你想要什么谢谢@Marc B。我让你的代码工作了,但它只返回1行结果,因为$array[$feed['value\u being\u fetched']]。如何更改$array以使用唯一id(例如xml文件中的productID)对所有结果进行整齐排序?