2011-05-13 92 views
0

我想要从XML文件中的数据,我需要以呼应产品检索XML数据

data.xml文件的属性值----

<products> 
<product id="123" /> 
</products> 

php文件---

$xml = new DomDocument(); 
$xmlFile = "data.xml";   
$xml= DOMDocument::load($xmlFile);   
$product = $xml->getElementsByTagName("product");  
foreach($product as $node)    
    {   
$id = $node->getElementsByAttributeName("id");   
$id = $address->item(0)->nodeValue;   
echo"$id";    
    } 
+0

啊,你应该引用你的XML数据:没办法告诉它是什么。 – Femi 2011-05-13 04:50:22

回答

0

使用getAttribute

$id = $node->getAttribute("id"); 
echo $id; 

,您可能还需要参考manual的,你需要等功能;)

1

我从来没有听说过的getElementsByAttributeName(),但如果你只想得到一个元素的属性,功能相当简单:

$xml = new DomDocument(); 
$xmlFile = "data.xml";   
$xml= DOMDocument::load($xmlFile);   
$product = $xml->getElementsByTagName("product"); 

foreach($product as $node) {   
    $id = $node->getAttribute("id");   
    echo $id;    
}