我想从XML文件中检索数据,我需要回显产品的属性值
数据.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";
}
我从未听说过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;
}
使用 getAttribute
:
$id = $node->getAttribute("id");
echo $id;
您可能还需要参考手册以了解您需要;)的其他功能