Friday, May 25, 2007

Read xml file using simplexml functions in PHP

first you have to create xml file


<ReportData>

<Record>

<CCode>client 2</CCode>

<DESTINATION>USA</DESTINATION>

<AIRWAYBILL>A222</AIRWAYBILL>

<AWB>AQ222</AWB>

<SUB>

<DATE>5/21/2007</DATE>

<TIME>12:24:04 PM</TIME>

</SUB>

</Record>



<Record>

<CCode>client 2</CCode>

<DESTINATION>USA</DESTINATION>

<AIRWAYBILL>A222</AIRWAYBILL>

<AWB>AQ222</AWB>

<SUB>

<DATE>5/21/2007</DATE>

<TIME>12:24:04 PM</TIME>

</SUB>

</Record>



<Record>

<CCode>client 2</CCode>

<DESTINATION>USA</DESTINATION>

<AIRWAYBILL>A222</AIRWAYBILL>

<AWB>AQ222</AWB>

<SUB>

<DATE>5/21/2007</DATE>

<TIME>12:24:04 PM</TIME>

</SUB>

</Record>

</ReportData>


now user this code for parsing read in with simplexml functions


 


if(!$xml=simplexml_load_file('report.xml'))

{

trigger_error('Error reading XML file',E_USER_ERROR);

echo "file loaded";

}

echo 'Displaying contents of XML file...<br />';

foreach($xml as $records){

echo '<B>CLINET CODE: </b>'.$records->CCode.'<B> DESTINATION: <b>'.$records->DESTINATION.'

<B>AIRWAY BILL: </b>'.$records->AIRWAYBILL.'<br />';

}


run it this will display result of cccode and destination and airwaybill

No comments: