I’m working on a project that requires an XML file to be parsed and saved to an xls file. The problem I’m having is that I can’t get the data from the XML file into the xls file. I was able to create the xls file using the code below:
<?php
header(“Content-type: application/vnd.ms-excel”);
header(“Content-Disposition: attachment;Filename=techdata.xls”);
echo “<html>”;
echo “<meta http-equiv=“Content-Type” content=“text/html; charset=Windows-1252”>”;
echo “<body>”;
echo "<h4>Location</h4>
";
echo “</body>”;
echo “</html>”;
?>
I stumbled across Jubba’s Xml PHP Parse Tutorial, but I can’t get it to parse the data from my XML file that has this structure:
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>
<techData xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”>
<tData>
<Facility>data</Facility>
<Weight>data</Weight>
<Volume>data</Volume>
<Mass>data</Mass>
<Length>Enter data here</Length>
<Height>Enter data here</Height>
<Halflife>Enter data here</Halflife>
</tData>
<tData>
<Facility>data</Facility>
<Weight>data</Weight>
<Volume>data</Volume>
<Mass>data</Mass>
<Length>Enter data here</Length>
<Height>Enter data here</Height>
<Halflife>Enter data here</Halflife>
</tData>
<tData>
<Facility>data</Facility>
<Weight>data</Weight>
<Volume>data</Volume>
<Mass>data</Mass>
<Length>Enter data here</Length>
<Height>Enter data here</Height>
<Halflife>Enter data here</Halflife>
</tData>
</techData>
My PHP file that does the parsing looks like this:
<?php
$xml_file = “techdata.xml”;
$xml_CountryState_key = “techDatatDataFacility";
$xml_PartyRes_key = "techDatatDataWeight”;
$xml_TestFac_key = “techDatatDataVolume";
$xml_CertReq_key = "techDatatDataMass”;
$xml_PkgReq_key = “techDatatDataLength";
$xml_CurReq_key = "techDatatDataHeight”;
$xml_FutReq_key = “techDatatData*Halflife”;
$tecdata_array = array();
$counter = 0;
class xml_tecdata{
var $Facilit, $Weigh, $Volu, $Mas, $CertificationReq, $Leng, $Heig, $Halfl;
}
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_Facilit_key, $xml_Weigh_key, $xml_Volu_key, $xml_Mas_key, $xml_Leng_key, $xml_Height_key, $xml_Halfl_key, $counter, $tecdata_array;
switch($current_tag){
case $xml_Facilit:
$tecdata_array[$counter] = new xml_tecdata();
$tecdata_array[$counter]->Facilit = $data;
break;
case $xml_Weigh_key:
$tecdata_array[$counter]->Weigh = $data;
$counter++;
break;
case $xml_Volu_key:
$tecdata_array[$counter]->Volu = $data;
$counter+++;
break;
case $xml_Mas_key:
$tecdata_array[$counter]->Mas = $data;
$counter++++;
break;
case $xml_len_key:
$tecdata_array[$counter]->Leng = $data;
$counter+++++;
break;
case $xml_height_key:
$tecdata_array[$counter]->height = $data;
$counter++++++;
break;
case $xml_Halfl_key:
$tecdata_array[$counter]->Halfl = $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”);
$data = fread($fp, 80000);
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>
<h1>Technical Data</h1>
</head>
<body>
<table cellspacing=‘6’ cellpadding=‘6’ border=“0”>
<tr>
<th><h4>Facility</h4></th>
<th><h4>Weight</h4></th>
<th><h4>Volume</h4></th>
<th><h4>Mass</h4></th>
<th><h4>length</h4></th>
<th><h4>Height</h4></th>
<th><h4>Halflife</h4></th>
</tr>
</table>
<?
for($x=0;$x<count($tecdata_array);$x+++++++){
echo . $tecdata_array[$x]->Facilit . "
";
echo . $tecdata_array[$x]->Weigh . "
";
echo . $tecdata_array[$x]->Volu . "
";
echo . $tecdata_array[$x]->Mas . "
";
echo . $tecdata_array[$x]->Leng . "
";
echo . $tecdata_array[$x]->height . "
";
echo . $tecdata_array[$x]->Halfl . "
";
}
?>
</body>
</html>
Any help you can provide will be appreciated. I’ve been at this for three straight day and cant seem to get it working.