Forum Moderators: coopster
Firstly, I am very new to PHP, so I don't have much knowledge of it.
What I am trying to do is parse an XML document into an array so that I can later on put the data into a database.
My XML file looks like this, for example:
<?xml version="1.0" encoding="utf-8"?>
<members>
<member>
<exchange_id>1</exchange_id>
<organisation>Test Organisation 1</organisation>
<f_name>Test First 1</f_name>
<l_name>Test Last 1</l_name>
<biz_mobile>0407081545</biz_mobile>
<biz_fax>68887799</biz_fax>
<categories>
<category>Test Category 1</category>
<category>Test Category 2</category>
<category>Test Category 3</category>
</categories>
</member>
</members>
There are more fields than that, and obviously more than 1 member.
Here is the PHP code that I believe is the important stuff:
--------------------------
$currentElements = array();
$newsArray = array();
readXml("test3.xml");
echo("<pre>");
print_r($newsArray);
echo("</pre>");
// Reads XML file into formatted html
function readXML($xmlFile)
{
$xmlParser = xml_parser_create();
xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($xmlParser, startElement, endElement);
xml_set_character_data_handler($xmlParser, characterData);
$fp = fopen($xmlFile, "r");
while($data = fread($fp, filesize($xmlFile))){
xml_parse($xmlParser, $data, feof($fp));}
xml_parser_free($xmlParser);
}
// Sets the current XML element, and pushes itself onto the element hierarchy
function startElement($parser, $name, $attrs)
{
global $currentElements, $itemCount;
array_push($currentElements, $name);
if($name == "member"){$itemCount += 1;}
}
// Prints XML data; finds highlights and links
function characterData($parser, $data)
{
global $currentElements, $newsArray, $itemCount;
$currentCount = count($currentElements);
$parentElement = $currentElements[$currentCount-2];
$thisElement = $currentElements[$currentCount-1];
if($parentElement == "member"){
$newsArray[$itemCount-1][$thisElement] = $data;
}
else{
switch($name){
case "exchange_id":
break;
case "organisation":
break;
case "f_name":
break;
case "l_name":
break;
case "job_title":
break;}}
}
// If the XML element has ended, it is poped off the hierarchy
function endElement($parser, $name)
{
global $currentElements;
$currentCount = count($currentElements);
if($currentElements[$currentCount-1] == $name){
array_pop($currentElements);}
}
--------------------------
This does work to a degree, the output that I get is:
Array
(
[0] => Array
(
[exchange_id] => 1
[organisation] => Test Organisation 1
[f_name] => Test First 1
[l_name] => Test Last 1
[biz_mobile] => 0407081545
[biz_fax] => 68887799
[categories] =>
)
)
Now what I need is to be able to put each category in the categories part of the array, separated by commas. Does anyone have any ideas how to do this?
Thanks guys, and sorry for my 'newbie' post...
Cheers,
Damian
-------------------------------------
if($parentElement == "categories"){
$newsArray[$itemCount-1][$thisElement] = $data;
}
-------------------------------------
This changes my output slightly to:
Array
(
[0] => Array
(
[exchange_id] => 1
[organisation] => Test Organisation 1
[f_name] => Test First 1
[l_name] => Test Last 1
[biz_mobile] => 0407081545
[biz_fax] => 68887799
[categories] =>
[category] => Test Category 3
)
)
Note that it is only outputting the last category of the 3 in the XML, where I need it to output all 3 of them, separated by commas. Just can't get my head around this... perhaps it's this flu I've been nursing all weekend :P
Thanks,
Damian