php - Get XML element attribute with simplexml -
i have xml file follows:
<?xml version="1.0" ?> <shop version="2.0" shop-name="xyz"> <category name="foo1"> <subcategory name="foobar1"> <product name="productname1" id="1"> <supplier name="xxx" logo="xxx.gif" /> <desc>desc</desc> </product> <product name="productname2" id="2"> ... </product> </subcategory> </category> </shop>
and attribute value of shop element - shop-name
i used simplexml in php:
<?php $dataxml=simplexml_load_file("data.xml"); $a=$dataxml->shop[0]["shop-name"]; echo $a; ?>
as result nothing. have idea wrong?
those attributes, need access them correctly attributes()
method:
$data = simplexml_load_file( 'data.xml' ); $attributes = $data->attributes(); echo $attributes['shop-name'];
or can access directly, since it's main attribute
:
$data = simplexml_load_file( 'data.xml' ); echo $data['shop-name'];
Comments
Post a Comment