php - getting attributes in simplexml_load_string -


below function using convert xml array

$xml    =   '<xml><codeshareinfo operatingcarrier="ey" operatingflightnumber="269">etihad airways</codeshareinfo></xml>'; $obj        =   simplexml_load_string($xml); // parse xml $obj->registerxpathnamespace("soap", "http://www.w3.org/2003/05/soap-envelope"); $array      =   json_decode(json_encode($obj), true); // convert array 

when try parent node "xml"

<xml><codeshareinfo operatingcarrier="ey" operatingflightnumber="269">etihad airways</codeshareinfo></xml> 

i result

array (     [codeshareinfo] => etihad airways ) 

but if try without parent node "xml"

<codeshareinfo operatingcarrier="ey" operatingflightnumber="269">etihad airways</codeshareinfo> 

i can attributes , value

array (     [@attributes] => array         (             [operatingcarrier] => ey             [operatingflightnumber] => 269         )     [0] => etihad airways ) 

what should change in code output attributes , values xml data soap request , once receive convert array access value , attributes.

given provided xml can iterate through , pull out attributes , values.

<?php $xml    =   '<xml><codeshareinfo operatingcarrier="ey" operatingflightnumber="269">etihad airways</codeshareinfo></xml>'; $obj        =   simplexml_load_string($xml); // parse xml $obj->registerxpathnamespace("soap", "http://www.w3.org/2003/05/soap-envelope"); foreach($obj $ob) {     echo $ob['operatingcarrier'] . "\n";     echo $ob . "\n";     echo $ob['operatingflightnumber'] . "\n"; } 

output:

ey
etihad airways
269


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -