Jump to content

Parsing XML with PHP - Table not formatting correctly


lynsey76

Recommended Posts

Hi guys

I've been asked to parse & style an XML feed. This is what I have so far, it's nearly working just the table layout is a bit wonky.

<?php

$xml_file = "arr.xml";

$xml_origdest_key = "*ARRIVALS*ARRIVAL*ORIGDEST";
$xml_schedtime_key = "*ARRIVALS*ARRIVAL*SCHEDTIME";
$xml_probtime_key = "*ARRIVALS*ARRIVAL*PROBTIME";
$xml_message_key = "*ARRIVALS*ARRIVAL*MESSAGE";

$arrival_array = array();

$counter = 0;
class xml_arrival{
var $origdest, $schedtime, $probtime, $message;
}

function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}

function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}

function contents($parser, $data){
global $current_tag, $xml_origdest_key, $xml_schedtime_key, $xml_probtime_key, $xml_message_key, $counter, $arrival_array;
switch($current_tag){
case $xml_origdest_key:
$arrival_array[$counter] = new xml_arrival();
$arrival_array[$counter]->origdest = $data;
break;
case $xml_schedtime_key:
$arrival_array[$counter]->schedtime = $data;
$counter++;
break;
case $xml_probtime_key:
$arrival_array[$counter]->probtime = $data;
$counter++;
break;
case $xml_message_key:
$arrival_array[$counter]->message = $data;
$counter++;
break;
}
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($xml_file, "r") or die("Could not open file");

$data = fread($fp, filesize($xml_file)) or die("Could not read file");

if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

?>

<html>
<head>
<title></title>
<style type="text/css">
body { font-family: Arial, Helvetica, sans-serif;}
</style>
</head>
<body bgcolor="#FFFFFF">
<?php
echo "<TABLE WIDTH='600' CELLSPACING='0' CELLPADDING='0' BORDER='1'>";
echo "<TR>";
echo "<TH>Flight</TH>";
echo "<TH>Origdest</TH>";
echo "<TH>schedtime</TH>";
echo "<TH>probtime</TH>";
echo "<TH>message</TH>";
echo "</TR>";
for($x=0;$x<count($arrival_array);$x++)
{
echo "<TR>";
echo "<TD>" . $arrival_array[$x]->fltnmbr . "</TD>";
echo "<TD>" . $arrival_array[$x]->origdest . "</TD>";
echo "<TD>" . $arrival_array[$x]->schedtime . "</TD>";
echo "<TD>" . $arrival_array[$x]->probtime . "</TD>";
echo "<TD>" . $arrival_array[$x]->message . "</TD>";
echo "</TR>";
}
echo "</TABLE>";
?>

</body>
</html>

It only displays the first 2 columns then starts displaying column 3 on the line below???

Any help would be appreciated.

Cheers

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.