By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,463 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,463 IT Pros & Developers. It's quick & easy.

XML Array

P: n/a
Ok Perhaps, I am going about this the wrong way, but here is what I
have.
$string_data = array();
$data_flag = 0; //data we want printed flag ... which is everything
that is entered in switch()
$desc_flag = 0; //desc element flag
$end_flag = 0; //end element flag
$prog_flag = 0; //programme element flag
$strt_flag = 0; //start element flag
$titl_flag = 0; //title element flag
$list_flag = 0; //Programme Listing Finish

function startElement($parser, $element_name, $element_attrs)
{
global $data_flag, $desc_flag, $end_flag, $prog_flag, $strt_flag,
$titl_flag, $list_flag;
switch($element_name)
{
case "programme":
$prog_flag = 1;
$list_flag = 1;
break;
case "title":
$titl_flag = 1;
$data_flag = 1;
break;
case "desc":
$desc_flag = 1;
$data_flag = 1;
break;
case "start":
$strt_flag = 1;
$data_flag = 1;
break;
case "end":
$end_flag = 1;
$data_flag = 1;
break;
default :
$data_flag = 0;
break;
}
}

function endElement($parser, $element_name)
{
global $data_flag, $desc_flag, $end_flag, $prog_flag, $strt_flag,
$titl_flag, $list_flag, $string;
switch($element_name)
{
case "programme":
$prog_flag = 0;
$list_flag = 0;
break;
case "title":
$titl_flag = 0;
$data_flag = 0;
break;
case "desc":
$desc_flag = 0;
$data_flag = 0;
break;
case "start":
$strt_flag = 0;
$data_flag = 0;
break;
case "end":
$end_flag = 0;
$data_flag = 0;
break;
default :
$data_flag = 0;
break;
}
}

function charData($parser,$data)
{
global $data_flag, $desc_flag, $end_flag, $prog_flag, $strt_flag,
$titl_flag, $list_flag;
if ($data_flag == 1){
if ($desc_flag == 1){
$string['desc'] = "$data<br />\n";
}
if ($end_flag == 1){
$string['end'] = "$data<br />\n";
}
if ($strt_flag == 1){
$string['strt'] = "$data<br />\n";
}
if ($titl_flag == 1){
$string['titl'] = "$data<br />\n";
}
}
if ($list_flag == 1){
if (is_array ($string)){
print "{$string['strt']}";
print "{$string['end']}";
}
}
}

This is for a tv listing with the following as a sample entry
<programme>
<desc>Start your day with all the latest news, sport, business and
weather from the BBC's Breakfast team. Includes regional news at 25 and
55 minutes past each hour.</desc>
<title>Breakfast</title>
<end>0915</end>

<infourl>http://www.bbc.co.uk/cgi-perl/whatson/prog_parse.cgi?FILENAME=20050107/20050107_0600_4223_34428_195</infourl>

<start>0600</start>
</programme>
Now I would like to have this information listed as
start end
title
desc

However, as I have it now, it simply prints out as it is parsed. ie.
desc
title
end
start

Any Ideas, hints, suggestions? I would love to use XSLT for this, but
unfortunately, the client does not want to. (grumble)

TFTH
Ron Chaplin
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
T73 Software & Design
www.t73-softdesign.com
To provide custom and quality
software, designs and services,
to our customers, at an affordable rate,
with minimal delay.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ro***********@gmail.com wrote:
Any Ideas, hints, suggestions? I would love to use XSLT for this, but
unfortunately, the client does not want to. (grumble)


One way of doing this:

<?php

<?

$order = array("start", "end", "title", "desc");
$currentElement = "";
$temp = array();
$index = 0;

function startElement($parser, $name, $attrs) {
global $order, $index, $currentElement, $temp;

$currentElement = $name;
if (false !== ($index = array_search($name, $order))) {
$temp[$index] = '';
}
}

function endElement($parser, $name) {
global $index, $temp, $order;

if (count($temp) == count($order)) {
sort($temp);
foreach ($temp as $cdata) {
print "$cdata<br /><br />\n";
}
$temp = array();
}
}

function characterData($parser, $data) {
global $temp, $order, $currentElement, $index;

if (false !== array_search($currentElement, $order)) {
if (isset($temp[$index])) {
$temp[$index] .= $data;
}
}
}

?>
JW

Jul 17 '05 #2

P: n/a
<ro***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
[ ... ]
Any Ideas, hints, suggestions? I would love to use XSLT for this, but
unfortunately, the client does not want to. (grumble)


You might want to use xml_parse_into_struct() instead of using the expat
functions directly. The manual has a good example on how to take the array
and convert it into an array of objects. Once you get it into that form,
getting the right output is easy.

http://www.php.net/xml_parse_into_struct/
Jul 17 '05 #3

P: n/a
Janwillem Borleffs wrote:
function endElement($parser, $name) {
global $index, $temp, $order;

if (count($temp) == count($order)) {
sort($temp);
foreach ($temp as $cdata) {
print "$cdata<br /><br />\n";
}
$temp = array();
}
}


Worked Great except for one minor detail. It should be ksort($temp); and
not just sort($temp); so that it sorts by key. Other than that works
great. results can be seen @ http://www.t73-softdesign.com/tv_xml/tv_xml.php

Thanks Again

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.