retrieving xml data
i want to retrieve data from xml file, i need to echo the attribute valu开发者_如何学Goe of product
data.xml file----
<products>
<product id="123" />
</products>
php file---
$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";
}
I've never heard of getElementsByAttributeName()
, but if you want to just get the attribute of an element, the function is quite simple:
$xml = new DomDocument();
$xmlFile = "data.xml";
$xml= DOMDocument::load($xmlFile);
$product = $xml->getElementsByTagName("product");
foreach($product as $node) {
$id = $node->getAttribute("id");
echo $id;
}
Use getAttribute
:
$id = $node->getAttribute("id");
echo $id;
You may also want to refer to the manual for other functions that you need ;)
精彩评论