如何获取XML属性?

问题描述:

我试图抓住几种产品的数据,然后列出每种产品的属性。我结束了来自Attribute id =“259”的相同属性。谁能解决我的问题吗? XML实例如何获取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 

}} 

尝试

$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'];