PHP and XML: How to retrieve attributes in XML -


here xml:

<?xml version="1.0" encoding="utf-8" ?> <items>   <itemgroup name="atm" column="1" sort="0">     <item formid="atm 01" description="option teller balance sheet - atm dept. ordering only" unit="cello 500"/>   </itemgroup>   <itemgroup name="data processing" column="1">     <item formid="aacu 01" description="receipt voucher - 2 part" unit="3600/box"/>     <item formid="aacu 04" description="contract collection statement" unit="as req."/>     <item formid="aacu 07" description="1-part receipt voucher - cont. feed form" unit="5000/box"/>     <item formid="aacu 08" description="share ira certificate" unit="2200/box"/>     <item formid="aacu 15" description="pta pin mailer" unit="1800/box"/>     <item formid="aacu 15b" description="online access pin mailer" unit="2500/box"/>   </itemgroup> </items> 

i know need use code such this:

$xml_file = "aacu-data.xml"; $xml = simplexml_load_file($xml_file); 

but need know how can retrieve attributes such description , unit formid="aacu 07" , able assign 2 variables such $description , $unit.

with modifications got work:

$xml_file = "aacu-data.xml"; $xml = simplexml_load_file($xml_file);  foreach($xml->itemgroup $itemgroup) {     foreach($itemgroup->children() $items) {          echo $items->attributes()->formid . "<br>";         echo $items->attributes()->description . "<br>";         echo $items->attributes()->unit . "<br>";      } }: 

you can access value using attributes() method

foreach($xml->itemgroup[1]->attributes() $a => $b) {     .... } 

to convert xml array

function xml2array($xmlobject){      $out = array();      foreach ( (array) $xmlobject $index => $node ){           if( is_object($node) , empty($node)){             $out[$index] = '';         }else{             $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;         }     }     return $out; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -