如何从xml匹配产品ID属性,然后将产品元素放入数组中?


How can I match product ID attribute from my xml and then put the product elements into an array

我有如下结构的xml,我想用php循环xml并匹配产品id属性。如果我得到匹配,我想将product元素添加到数组中,并从函数返回它们。

<Includes>
    <Products>
        <Product id="11348613">
             <name>Product A</name>
             <description>Product A's description</description>
             <image>http://www.domain.com/images/productA.jpg</image>
         </Product>
        <Product id="11348614">
             <name>Product B</name>
             <description>Product B's description</description>
             <image>http://www.domain.com/images/productB.jpg</image>
         </Product>
    </Products>
</Includes>

到目前为止,我所拥有的代码如下,这是如何不完全工作,因为我不认为我理解如何匹配属性,然后返回元素。你能指出我错在哪里吗?

function lookupSkuFromProdId($ProductId) {
    $reviewsXML = simplexml_load_file('reviews.xml');
    foreach ($reviewsXML->Includes->Products->attributes() as $sku) {
        if ($sku == $ProductId) {   
            $skuData = array();
            $skuData[name] = $sku->name;
            $skuData[description] = $sku->description;
            $skuData[image] = $sku->image;    
        }
    return $skuData;
}  

下面的代码片段可以使用XPath匹配使用产品ID的产品。

请记住我的代码中的一些细微的变化:

  1. 您的XML无效,因为它在</Products>结束标记之前有一个额外的</Product>结束标记;
  2. $ProductId变量重命名为$productId以保持CamelCase的一致性;
  3. 添加SimpleXMLElement变量作为lookupSkuFromProdId函数的第二个参数,但是您可以像以前一样从文件中加载它。
<?php
$xml = <<<XML
<Includes>
    <Products>
        <Product id="11348613">
            <name>Product A</name>
            <description>Product A's description</description>
            <image>http://www.domain.com/images/productA.jpg</image>
        </Product>
        <Product id="11348614">
            <name>Product B</name>
            <description>Product B's description</description>
            <image>http://www.domain.com/images/productB.jpg</image>
        </Product>
    </Products>
</Includes>
XML;
$reviewsXML = new SimpleXMLElement($xml);
$productId  = 11348613;
function lookupSkuFromProdId($productId, $reviewsXML) {
    $skuData = array();
    // Loading it from a PHP variable for this example only
    // Just uncomment this and load it as before
    // $reviewsXML = simplexml_load_file('reviews.xml');
    $sku = $reviewsXML->xpath("//Product[@id = '{$productId}']");
    if (!empty($sku)) {
        $skuData[name]        = (string) $sku[0]->name;
        $skuData[description] = (string) $sku[0]->description;
        $skuData[image]       = (string) $sku[0]->image;
    }
    return $skuData;
}
$skuData = lookupSkuFromProdId($productId, $reviewsXML);
print_r($skuData);

输出:

Array
(
    [name] => Product A
    [description] => Product A's description
    [image] => http://www.domain.com/images/productA.jpg
)

修复了一些问题,包括…缺少结束大括号,循环通过<Product>元素而不是属性,然后检查循环内的属性值,并在找到匹配时中断…

function lookupSkuFromProdId($ProductId) {
    $reviewsXML = simplexml_load_file('reviews.xml');
    foreach ($reviewsXML->Products->Product as $sku) {
        if ($sku['id'] == $ProductId) {  
            $skuData = array();
            $skuData['name'] = (string)$sku->name;
            $skuData['description'] = (string)$sku->description;
            $skuData['image'] = (string)$sku->image;
            break;    
        }
    }
    return $skuData;
} 

参见类似代码的演示:codepad.viper-7.com/8ONLaK

我看到您的方法有几个问题,您没有正确地循环xml树,您需要将xml对象转换为字符串以便将其值保存到数组中,并且您在数组索引周围缺少括号和引号。我已经修改了你的代码,试试这个:

function lookupSkuFromProdId($ProductId) {
    $reviewsXML = simplexml_load_file('reviews.xml');
    $skuData = array();
    foreach($reviewsXML->Products[0]->Product as $sku) {
        if ($sku['id'] == $ProductId){
            $skuData['name'] = (string)$sku->name;
            $skuData['description'] = (string)$sku->description;
            $skuData['image'] = (string)$sku->image;    
        }
    }
    return $skuData;
}

希望有帮助!