# XML Parsing using PHP

**URL:** https://forum.kirupa.com/t/xml-parsing-using-php/314499
**Category:** programming
**Created:** [September 30, 2010, 11:29am UTC](https://forum.kirupa.com/t/xml-parsing-using-php/314499 "2010-09-30T11:29:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![NewbAppDev](https://avatars.discourse-cdn.com/v4/letter/n/ecc23a/32.png) [@NewbAppDev](https://forum.kirupa.com/u/NewbAppDev)
#### Post date: [September 30, 2010, 11:29am UTC](https://forum.kirupa.com/t/xml-parsing-using-php/314499/1 "2010-09-30T11:29:49Z")

</div>

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](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 = “_techData_tData_Facility";  
$xml\_PartyRes\_key = "techDatatData_Weight”;  
$xml\_TestFac\_key = “_techData_tData_Volume";  
$xml\_CertReq\_key = "techDatatData_Mass”;  
$xml\_PkgReq\_key = “_techData_tData_Length";  
$xml\_CurReq\_key = "techDatatData_Height”;  
$xml\_FutReq\_key = “_techData_tData\*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&lt;count($tecdata_array);$x+++++++){
echo . $tecdata_array[$x]-&gt;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.
