2012-03-21 230 views
0

我試圖抓住幾種產品的數據,然後列出每種產品的屬性。我結束了來自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 

}} 

回答

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