我正在尝试使用以下xml的simplexml_load_string和SimpleXMLElement使用php函数获取product(despecid)节点的属性。
<productlist brand="HP" model="Color Laserjet 3000">
<product despecid="CAN21679">IR C1021/MF8450 Fuser Unit (Fixing Assembly)</product>
<product despecid="HPRM1-2743-180CN">CLJ 3000/3600/3800 CP3505 fuser unit</product>
<product despecid="XER003R99755">Xerox XRC toner Q7560A black</product>
</productlist>
但我从(simplexml_load_string和SimpleXMLElement)方法输出中得出如下所示。
object(SimpleXMLElement)#179 (2) {
["@attributes"]=>
array(2) {
["Brand"]=>
string(2) "HP"
["Model"]=>
string(19) "Color Laserjet 3000"
}
["Product"]=>
array(6) {
[0]=>
string(44) "IR C1021/MF8450 Fuser Unit (Fixing Assembly)"
[1]=>
string(36) "CLJ 3000/3600/3800 CP3505 fuser unit"
[2]=>
string(28) "Xerox XRC toner Q7560A black"
[3]=>
string(27) "Xerox XRC toner Q7561A cyan"
[4]=>
string(29) "Xerox XRC toner Q7562A yellow"
[5]=>
string(30) "Xerox XRC toner Q7563A magenta"
}
}
是否有任何方法可以获得产品的特性。请帮忙......
答案 0 :(得分:3)
$xml = <<<XML
<productlist brand="HP" model="Color Laserjet 3000">
<product despecid="CAN21679">IR C1021/MF8450 Fuser Unit (Fixing Assembly)</product>
<product despecid="HPRM1-2743-180CN">CLJ 3000/3600/3800 CP3505 fuser unit</product>
<product despecid="XER003R99755">Xerox XRC toner Q7560A black</product>
</productlist>
XML;
foreach (simplexml_load_string($xml)->product as $product) {
print_r($product->attributes()->despecid);
}
请参阅Demo