parseing xml response through php -
this question has answer here:
- basic simplexml working example? 5 answers
simplexmlelement object ( [flightoption] => array ( [0] => simplexmlelement object ( [flight] => simplexmlelement object ( [@attributes] => array ( [arrivaldatetime] => 2014-07-05t15:00:00.000 [departuredatetime] => 2014-07-05t13:00:00.000 [equipmentcode] => 321 [flightnumber] => 677 ) [airline] => simplexmlelement object ( [@attributes] => array ( [airlinecode] => ai [iatacode] => ai [icaocode] => aic [name] => air india ) ) [arrivalairport] => simplexmlelement object ( [@attributes] => array ( [airportcode] => del [iatacode] => del [icaocode] => vidp [name] => indira gandhi international airport ) ) [cabin] => array ( [0] => simplexmlelement object ( [@attributes] => array ( [code] => c [name] => coach ) [fareclass] => array ( [0] => simplexmlelement object ( [@attributes] => array ( [code] => y [name] => economy [numofavailableseats] => 9 ) ) ) ) [1] => simplexmlelement object ( [@attributes] => array ( [code] => b [name] => business ) [fareclass] => array ( [0] => simplexmlelement object ( [@attributes] => array ( [code] => c [name] => business [numofavailableseats] => 4 ) ) ) ) ) ) ) ) )
i have parse above xml response. please me that.
i using below code giyves me error code , not getting values in variable.
foreach ($data->flightoption $flightoption) { printf( $flightoption["flight"]->arrivaldatetime /*$flightoption->fareclass, $flightoption->airline*/ ); }
assuming xml looks this:
<?xml version="1.0"?> <data> <flightoption> <flight arrivaldatetime="2014-07-05t15:00:00.000" departuredatetime="2014-07-05t13:00:00.000" equipmentcode="321" flightnumber="677"> <airline airlinecode="ai" iatacode="ai" icaocode="aic" name="air india" /> <arrivalairport airportcode="del" iatacode="del" icaocode="vidp" name="indira gandhi international airport" /> <cabin code="c" name="coach"> <fareclass code="y" name="economy" numofavailableseats="9" /> </cabin> <cabin code="b" name="business"> <fareclass code="c" name="business" numofavailableseats="4" /> </cabin> </flight> </flightoption> </data>
it looks problem there multiple fareclass
elements in each flightoption
element. require nested loop inside first loop. following code:
<?php $data = simplexml_load_file("data.xml"); foreach($data->flightoption $flightoption) { $arrivaldatetime = $flightoption->flight["arrivaldatetime"]; $airline = $flightoption->flight->airline["name"]; foreach($flightoption->flight->cabin $cabin) { print($arrivaldatetime); print(" / "); print($cabin->fareclass["name"]); print(" / "); print($airline); print("<br/><br/>"); } print("<br/><br/>"); } ?>
will produce following output:
2014-07-05t15:00:00.000 / economy / air india 2014-07-05t15:00:00.000 / business / air india
note: making assumptions here, because not have enough rep post comment asking more information. if doesn't answer question, please give more information (like error getting), , try edit answer accordingly.
Comments
Post a Comment