如何获取XML属性?

时间:2012-03-21 04:31:52

标签: php xml

我正在尝试获取多个产品的数据,然后列出每个产品的属性。我最终得到了属性id =“259”的相同属性。有谁能解决我的问题? XML示例

    <AttributeValue id="258888">
<name>HON</name>
</AttributeValue>
<AttributeValue id="276682">
<name>Office Star</name>
</AttributeValue>

<Attribute id="296935">
<name>Stores</name>
<AttributeValues>
<AttributeValue id="18893">
<name>Test</name>
</AttributeValue>
</AttributeValues>
</Attribute>
</RelatedAttributes>

    foreach($xml->RelatedAttributes->Attribute as $attribute){
echo $attribute->name; //main Attribute Name

foreach($xml->RelatedAttributes->Attribute->AttributeValues->AttributeValue as $attribute_values){
echo $attribute_values->name; //subtopic for main Attribute

}}

1 个答案:

答案 0 :(得分:0)

尝试

$xml=simplexml_load_file($xml_file);
foreach($xml->AttributeValue[0]->attributes() as $key => $value) {
    echo $a,'="',$b,"\"\n";
}

还试一试

$attr = $xml->AttributeValue[0]->attributes();
echo $attr['id'];