开发者

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 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜