Page 1 of 1

How to parsing XML by using PHP4

Posted: Tue May 08, 2007 11:45 am
by hkidea1668
Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I got the XML format as show below, I want to know how can I 
parsing this XML by using PHP4 and display the output in HTML table?

Description of this XML file: 
Each flight will have many cabin name


[syntax="xml"]<Flight>
  <AirCode>ABC Air Line Company</AirCode> 
  <DepartureAirport>Beijing</DepartureAirport> 
  <DestinationAirport>Guangzhou</DestinationAirport> 
  <AirLine>CA1351</AirLine> 
  <Planestyle>738</Planestyle> 
  <Arritime>1055</Arritime> 
  <Deptime>0750</Deptime> 
  <FlyDate>2007-5-18 0:00:00</FlyDate> 
  <TravelTime>03:05</TravelTime> 
  <IsEtkt>True</IsEtkt> 
  <AirPortTax>50</AirPortTax> 
  <FuelTax>80.0</FuelTax> 
- <Cabins>
- <Cabin>
  <CabinName>L</CabinName> 
  <CabinType>70% (L)</CabinType> 
  <AdultPrice>1190.00</AdultPrice> 
  <ChildiePrice>850</ChildiePrice> 
  <InfantPrice>170</InfantPrice> 
  <TicketNumber>A</TicketNumber> 
  </Cabin>
- <Cabin>
  <CabinName>K</CabinName> 
  <CabinType>75%(K)</CabinType> 
  <AdultPrice>1280.00</AdultPrice> 
  <ChildiePrice>850</ChildiePrice> 
  <InfantPrice>170</InfantPrice> 
  <TicketNumber>A</TicketNumber> 
  </Cabin>
- <Cabin>
  <CabinName>H</CabinName> 
  <CabinType>80%(H)</CabinType> 
  <AdultPrice>1360.00</AdultPrice> 
  <ChildiePrice>850</ChildiePrice> 
  <InfantPrice>170</InfantPrice> 
  <TicketNumber>A</TicketNumber> 
  </Cabin>
- <Cabin>
  <CabinName>M</CabinName> 
  <CabinType>85%(M)</CabinType> 
  <AdultPrice>1450.00</AdultPrice> 
  <ChildiePrice>850</ChildiePrice> 
  <InfantPrice>170</InfantPrice> 
  <TicketNumber>A</TicketNumber> 
  </Cabin>
- <Cabin>
  <CabinName>B</CabinName> 
  <CabinType>90%(B)</CabinType> 
  <AdultPrice>1530.00</AdultPrice> 
  <ChildiePrice>850</ChildiePrice> 
  <InfantPrice>170</InfantPrice> 
  <TicketNumber>A</TicketNumber> 
  </Cabin>
- <Cabin>
  <CabinName>Y</CabinName> 
  <CabinType>0%(Y)</CabinType> 
  <AdultPrice>1700.00</AdultPrice> 
  <ChildiePrice>850</ChildiePrice> 
  <InfantPrice>170</InfantPrice> 
  <TicketNumber>A</TicketNumber> 
  </Cabin>
- <Cabin>
  <CabinName>F</CabinName> 
  <CabinType>First Class(F)</CabinType> 
  <AdultPrice>2550.00</AdultPrice> 
  <ChildiePrice>1280</ChildiePrice> 
  <InfantPrice>260</InfantPrice> 
  <TicketNumber>A</TicketNumber> 
  </Cabin>
  </Cabins>
</Flight>
The coding I am using like this, but it doesn't create the loop for each cabin[/syntax]

Code: Select all

$usercount=0;  // to hold the no. of flight found
$cabincount=0; // to hold the no. of cabin found inside each flight
$userdata=array(); // fill with data for each flight
$state=''; // keep track of which node the parser is dealing with for each flight

//-------------------------------------------------------------start
function startElementHandler ($parser,$name,$attrib){
global $usercount;
global $cabincount;
global $userdata;
global $state;

switch ($name) {
	case $name=="CABIN" : {
	$userdata[$cabincount]["CABINNAME"] = $attrib["CABINNAME"];
	break;
}

default : {$state=$name;break;}
}
}
//--------------------------------------------------------------end
function endElementHandler ($parser,$name){
global $usercount;
global $cabincount;
global $userdata;
global $state;
$state='';

if($name=="FLIGHT") {$usercount++;}
//}

if($name=="CABIN") {$cabincount++;}
}

//-------------------------------------------------------------datahandler
function characterDataHandler ($parser, $data) {
global $usercount;
global $cabincount;
global $userdata;
global $state;

if (!$state) {return;}

if ($state=="AIRCODE") { $userdata[$usercount]["AIRCODE"] = $data;}
if ($state=="DEPARTUREAIRPORT") { $userdata[$usercount]["DEPARTUREAIRPORT"] = $data;}
if ($state=="DESTINATIONAIRPORT") { $userdata[$usercount]["DESTINATIONAIRPORT"] = $data;}
if ($state=="AIRLINE") { $userdata[$usercount]["AIRLINE"] = $data;}
if ($state=="PLANESTYLE") { $userdata[$usercount]["PLANESTYLE"] = $data;}
if ($state=="ARRITIME") { $userdata[$usercount]["ARRITIME"] = $data;}
if ($state=="DEPTIME") { $userdata[$usercount]["DEPTIME"] = $data;}
if ($state=="FLYDATE") { $userdata[$usercount]["FLYDATE"] = $data;}
if ($state=="TRAVELTIME") { $userdata[$usercount]["TRAVELTIME"] = $data;}
if ($state=="ISETKT") { $userdata[$usercount]["ISETKT"] = $data;}
if ($state=="AIRPORTTAX") { $userdata[$usercount]["AIRPORTTAX"] = $data;}
if ($state=="FUELTAX") { $userdata[$usercount]["FUELTAX"] = $data;}
if ($state=="CABINNAME") { $userdata[$usercount]["CABINNAME"] = $data;}
if ($state=="CABINTYPE") { $userdata[$usercount]["CABINTYPE"] = $data;}
if ($state=="ADULTPRICE") { $userdata[$usercount]["ADULTPRICE"] = $data;}
if ($state=="CHILDPRICE") { $userdata[$usercount]["CHILDPRICE"] = $data;}
if ($state=="INFANPRICE") { $userdata[$usercount]["INFANPRICE"] = $data;}
if ($state=="TICKETNUMBER") { $userdata[$usercount]["TICKETNUMBER"] = $data;}
}

if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
xml_set_element_handler( $xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler( $xml_parser, "characterDataHandler");

while( $data = fread($fp, 80000)){
if(!xml_parse($xml_parser, $data, feof($fp))) {
break;}}
xml_parser_free($xml_parser);

?>
<html>
<head><title>China Air Fare Engine</title>
</head>
<body>
<?php

for ($i=0;$i<$usercount; $i++)
{ 
        echo "AirCode: ".$userdata[$i]["AIRCODE"];
		echo "<br>";
		echo "Departure Airport: ".$userdata[$i]["DEPARTUREAIRPORT"];
		echo "<br>";
		echo "Cabin Name: ".$userdata[$j]["CABINNAME"];
			
		echo "<br>";
		echo "<br>";
}
?>
</table>
</body><html>
Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue May 08, 2007 1:06 pm
by pickle
If you just want to output the contents of the file, try using XSLT.