我想从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 :(得分: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;
}
答案 1 :(得分:0)