XML 复杂解析 [英] XML complex Parsing
问题描述
可能的重复:
在 Xpath 中不同吗?
我正在尝试使用 PHP 从我的 XML 文件中提取数据.我想从我的 XML 文件中获取基于 WebCategory
的唯一 ProductRange
.但是下面编写的 PHP 代码会生成重复/重复的结果.我不知道我哪里出错了!这是代码:
I am trying to extract data from my XML file using PHP. I want to get unique ProductRange
based on WebCategory
from my XML file. But the PHP code written below generate duplicate/repetitive results. I don't know where I am making mistake! Here is the code :
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>
我想从我的 XML 文件中获取基于 WebCategory
的唯一 ProductRange
.但是下面编写的 PHP 代码会生成重复/重复的结果.我不知道我哪里出错了!
I want to get unique ProductRange
based on WebCategory
from my XML file. But the PHP code written below generate duplicate/repetitive results. I don't know where I am making mistake!
PHP 代码:
<?
$xml = simplexml_load_string(file_get_contents('XML/products.xml'));
$prifix = '/categories/listings/' ;
$cat=array();
foreach ($xml as $row) {
$attr = $row->attributes();
if (!in_array((string)$attr->WebCategory, $cat)){
printf('<li>%s</li>', anchor($prifix . $attr->Code, $attr->ProductRange));
$cat[] = (string)$attr->WebCategory;
}
}
?>
请注意:我想根据给定的 WebCategory
提取 ProductRange
例如我想根据他们的 Webcategory 显示所有 ProductRange,就像这样选择 SQL 查询:
PLEASE NOTE:
I want to extract ProductRange
based on given WebCategory
e.g I want to show all ProductRange according to their Webcategory like this select SQL query:
"select ProductRange from XML where WebCategory='Mobiles'"
它可以给我基于 XML 的独特ProductRange"(非重复结果),如下所示:
and it could give me distinct "ProductRange"(Not repetitive results) based on XML like this:
HTC
三星
手机
Htc
Samsung
Iphone
等等...我尽我所能,但未能使用上述编码方法生成独特的ProductRange".
and so... I tried my best but failed to generate unique "ProductRange" based using the coding approach mentioned above.
请纠正我的错误,并指导我在哪里需要进行更改以获得如上所述的唯一 ProductRange
.
Please correct me where i am wrong and kindly guide me where I need to make change to get unique ProductRange
as mentioned above.
推荐答案
你可以试试
$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;
}
这篇关于XML 复杂解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!