Php XML复杂解析

Php XML复杂解析,php,xml,Php,Xml,可能重复: 我正在尝试使用PHP从XML文件中提取数据。我想根据我的XML文件中的WebCategory获取唯一的ProductRange。但是下面编写的PHP代码会生成重复的结果。我不知道我在哪里犯了错误!代码如下: XML代码: <?xml version="1.0" standalone="yes"?> <Rows> <Row Code="10000" Name="HTC Wildfire S-A510E " ProductRange="HTC" Prod

可能重复:

我正在尝试使用PHP从XML文件中提取数据。我想根据我的XML文件中的
WebCategory
获取唯一的
ProductRange
。但是下面编写的PHP代码会生成重复的结果。我不知道我在哪里犯了错误!代码如下:

XML代码:

<?xml version="1.0" standalone="yes"?>
<Rows> 
<Row Code="10000" Name="HTC Wildfire S-A510E " ProductRange="HTC" ProductSubRange="Wildfire" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10001" Name="HTC Wildfire" ProductRange="HTC" ProductSubRange="Wildfire" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10002" Name="Samsung Galaxy S3" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10003" Name="Samsung Galaxy S2" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10004" Name="Samsung Galaxy S1" ProductRange="Samsung" ProductSubRange="Galaxy" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10005" Name="Samsung Galaxy Tabloid" ProductRange="Samsung" ProductSubRange="Galaxy Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10006" Name="Apple Ipad 3" ProductRange="Apple" ProductSubRange="Tabloids" WebCategory="Gadgets" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10007" Name="Apple Iphone 4S" ProductRange="Apple" ProductSubRange="Iphone" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10008" Name="Apple Iphone 3G" ProductRange="Apple" ProductSubRange="Iphone" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10009" Name="Miscrosoft XBOX 360 Elite" ProductRange="Microsoft" ProductSubRange="XBOX" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10010" Name="Sony Playstation 4" ProductRange="Sony" ProductSubRange="Playstation" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10011" Name="Sony PSP Go" ProductRange="Microsoft" ProductSubRange="PSP" WebCategory="Consoles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10012" Name="Sony Erricsson Satio" ProductRange="Sony Ericsson" ProductSubRange="Satio Series" WebCategory="Mobiles" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
<Row Code="10013" Name="TomTom Go Live Gl2" ProductRange="TomTom" ProductSubRange="Go Live" WebCategory="Navigation" WebDescription="Available in black and white.lightweight." Productlength="46mm" ProductWidth="16mm" ProductHeight="21.000" Weight="400gm" Description="Pck: 12   Plt: 1152" />
</Rows>
请注意: 我想根据给定的
WebCategory
提取
ProductRange
,例如,我想根据它们的WebCategory显示所有ProductRange,如下面的select SQL查询:

     "select ProductRange from XML where WebCategory='Mobiles'"
它可以基于XML为我提供不同的“ProductRange”(不是重复的结果),如下所示:

Htc
三星
Iphone

所以。。。我尽了最大努力,但未能使用上述编码方法生成唯一的“ProductRange”

请纠正我的错误,并请指导我在哪里需要进行更改以获得上述唯一的
ProductRange

您可以尝试

$list = groupBy($xml, "WebCategory");
foreach ( $list['Mobiles'] as $product ) {
    printf('<li>%s %s</li>', $product->Code, $product->Name);
}

xpath查询如何?该XML无效。您似乎缺少了
行的开始标记
@Quentin抱歉。我在编写时忘记添加xml。我已经按照您提到的进行了编辑。有什么方法可以避免记录中的重复吗?@Quentin请您帮个忙:回答得好,逻辑性强。。谢谢爸爸。我真的很感谢您的帮助。blessYou随时欢迎。。还有什么我可以帮你的@Bilal吗Khalid@Baba.i已经尝试过$xml=simplexml\u load\u字符串(file\u get\u contents('xml/products.xml');在使用groupBy函数之前,我正在嵌入外部XML文件,但它给了我错误。如果我使用查询字符串中的值,我需要做什么?我的意思是我正在使用codeigniter,希望从我的uri段中获取不同的WebCategory。我感谢您的帮助。bles只需使用
groupBy(file_get_contents('XML/products.XML'),“WebCategory”)
已修复为的代码namespace@Baba.Brilliant.祝福你,爸爸.你是个魔术师。
$list = groupBy($xml, "WebCategory");
foreach ( $list['Mobiles'] as $product ) {
    printf('<li>%s %s</li>', $product->Code, $product->Name);
}
function groupBy($xml, $categoryName) {
    $xml = new \SimpleXMLElement($xml);
    $category = array();
    foreach ( $xml as $row ) {
        $attr = $row->attributes();

        if (! isset($attr->$categoryName)) {
            trigger_error("$categoryName does not exist in XML");
            break;
        }

        $category[(string) $attr->$categoryName][] = $attr;
    }
    return $category;
}