How to parsing XML by using PHP4

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hkidea1668
Forum Newbie
Posts: 1
Joined: Tue May 08, 2007 11:41 am

How to parsing XML by using PHP4

Post 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]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If you just want to output the contents of the file, try using XSLT.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply