Sunday, January 6, 2013

Read xml file using DOM in PHP


<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>

</ReportData>

save above as report.xml file and than

run below script its script using dom how

you can read data from an xml file



<?php

$doc = new DOMDocument();

$doc->load( 'report.xml' );



$Record = $doc->getElementsByTagName( "Record" );

foreach( $Record as $rec )

{

$ccode = $rec->getElementsByTagName( "CCode" );

$ccode= $ccode->item(0)->nodeValue;



$destination = $rec->getElementsByTagName("DESTINATION");

$destination = $destination->item(0)->nodeValue;



$airwaybill = $rec->getElementsByTagName("AIRWAYBILL");

$airwaybill = $airwaybill->item(0)->nodeValue;



$awb = $rec->getElementsByTagName("AWB");

$awb = $awb->item(0)->nodeValue;



$SUB = $rec->getElementsByTagName( "SUB" );

$SUB = $SUB->item(0)->nodeValue;





$date = $rec->getElementsByTagName( "DATE" );

$date = $date->item(0)->nodeValue;

$time = $rec->getElementsByTagName( "TIME" );

$time = $time->item(0)->nodeValue;



echo "$ccode - $destination - $airwaybill - $awb - $SUB - $date - $time <BR>";

}

?>

No comments: