检索并解析与MySQL结果相关的XML记录

时间:2014-10-29 20:44:54

标签: php mysql xml simplexml

我试图从XML文件中获取信息,但我无法使其工作。 这是我的代码:

$xml = simplexml_load_file('http://www.website.de/file.xml');

$query  = "SELECT * FROM table WHERE shop='name' and datum='' LIMIT 5";          
$result = mysql_query($query) or die("Error: " . mysql_error());  
$db_erg = mysql_query( $query);
while ( $inhalt = mysql_fetch_array( $db_erg, MYSQL_ASSOC))
{
    $i = $inhalt['name'];
    $name = $xml->record[$i]->PRODUCT_NAME;
    $link = $xml->record[$i]->PRODUCT_LINK;
    $preis = $xml->record[$i]->PRICE;
    $versand = $xml->record[$i]->SHIPPING_COST;
    $ean = $xml->record[$i]->PRODUCT_CODE;
    $bild = $xml->record[$i]->PRODUCT_IMAGE_URL;
    $datum = time();
    $id = $inhalt['id'];
    echo "<p>".$id." geaendert.</p>";
}

每次我运行脚本时都会给我错误...

  

Trying to get property of non-object in...

...从$name$bild的行。所以我想它与$i有关但我真的不明白为什么这不起作用。 我将不胜感激。

P.S:我知道这不是一种使用MySQL的安全方法。完成网站上的脚本只会使用mysqli预处理语句。

这是我正在操作的XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<csv_data_records source="/var/www/network/domains/webservice/public_html/partner/functions/../../../../../tmp/productdata_cf45f01cb623aa26d26b25d362e11b56.tmp" datetime="2014-10-29T19:31:38+01:00" >
   <record>
      <PRODUCT_ID>9868.1</PRODUCT_ID>
      <PRODUCT_CODE>4032314007020</PRODUCT_CODE>
      <PRODUCT_CODE_TYPE>EAN</PRODUCT_CODE_TYPE>
      <PRODUCT_NAME>Luposan Kräuterkraft 30 Pulver - 1000 g</PRODUCT_NAME>
      <BRAND>Luposan</BRAND>
      <PRICE>22.99</PRICE>
      <PRICE_OLD>26.99</PRICE_OLD>
      <PRICE_CURRENCY>EUR</PRICE_CURRENCY>
      <CATEGORY>Hund &gt; Spezialfutter &amp; Nahrungsergänzungen &gt; Haut &amp; Fell &gt; Luposan</CATEGORY>
      <PRODUCT_IMAGE_URL>http://marketing.net.zooplus.de/ts/i3101179/tsv?tst=!!!TIME_STAMP!!!&amp;amc=aff.zooplus.20478.24084.8290&amp;rmd=1&amp;trg=http%3A%2F%2Fmedia.zooplus.com%2Fbilder%2F1%2F400%2F9868_luposan_kraeuterkraftb_1.jpg</PRODUCT_IMAGE_URL>
      <PRODUCT_LINK>http://marketing.net.zooplus.de/ts/i3101179/tsc?tst=!!!TIME_STAMP!!!&amp;amc=aff.zooplus.20478.24084.8290&amp;rmd=1&amp;trg=http%3A%2F%2Fwww.zooplus.de%2F-306243%2Fshop%2F%252B10PRFDEAFF%2Fhunde%2Fhundefutter_ergaenzung%2Fhaut_haare%2Fluposan%2F9868</PRODUCT_LINK>
      <AVAILABILITY>in stock</AVAILABILITY>
      <SHIPPING_COST>0.00</SHIPPING_COST>
      <PRODUCT_UNIT_PRICE>22.99</PRODUCT_UNIT_PRICE>
      <PRODUCT_UNIT_MEASURE>1</PRODUCT_UNIT_MEASURE>
      <PRODUCT_UNIT_BASE_VALUE>kg</PRODUCT_UNIT_BASE_VALUE>
 </record>

1 个答案:

答案 0 :(得分:2)

您发布的代码对我来说是正常的,所以我想问题是XML文件没有加载或者$i超出了XML文件中值的范围。使用添加到代码中的检查可以防止两者:

$xml = simpleXML_load_file($url, "SimpleXMLElement"); 
if ($xml === false) 
{ 
   die("Could not load the XML file!"); 
} 

$i = $inhalt['name'];
if (isset ($xml->record[$i])) {
    // do your processing
}
else {
    // trigger an error / warning
}

如果要收集大量产品的数据,您可能会发现将其存储在数组中更为有效,该数组由元素名称索引。它至少可以让你不必编写类似语句的负载。

您还可以使用simpleXML's XPath implementation通过将record与数据库中存储的ID相匹配来查找XML文件中的相关PRODUCT_ID。这是一个例子:

$data = array();
$prod_id = $inhalt['artikelnummer'];

# this finds the record nodes where the value of PRODUCT_ID is $prod_id
# [.='$prod_id'] means the node's text content should match $prod_id
# the /.. at the end of the query returns the parent node, i.e. the record
foreach ($xml->xpath("record/PRODUCT_ID[.='$prod_id']/..") as $e) {
    foreach( array('PRODUCT_NAME', 'PRODUCT_LINK', 'PRICE', 'SHIPPING_COST',
        'PRODUCT_CODE', 'PRODUCT_IMAGE_URL') as $x ) {
        # make sure that the relevant value element is there!
        if (isset($e->$x)) {
            $data[$x] = (string)$e->$x;
        }
    }
    print_r($data);
    // do whatever you want with $data
}

输出:

Array
(
    [PRODUCT_NAME] => Luposan Kräuterkraft 30 Pulver - 1000 g
    [PRODUCT_LINK] => http://marketing.net.zooplus.de/ts/i3101179/tsc?tst=!!!TIME_STAMP!!!&amc=aff.zooplus.20478.24084.8290&rmd=1&trg=http%3A%2F%2Fwww.zooplus.de%2F-306243%2Fshop%2F%252B10PRFDEAFF%2Fhunde%2Fhundefutter_ergaenzung%2Fhaut_haare%2Fluposan%2F9868
    [PRICE] => 22.99
    [SHIPPING_COST] => 0.00
    [PRODUCT_CODE] => 4032314007020
    [PRODUCT_IMAGE_URL] => http://marketing.net.zooplus.de/ts/i3101179/tsv?tst=!!!TIME_STAMP!!!&amc=aff.zooplus.20478.24084.8290&rmd=1&trg=http%3A%2F%2Fmedia.zooplus.com%2Fbilder%2F1%2F400%2F9868_luposan_kraeuterkraftb_1.jpg
)