473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Array

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
3 1935
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2755
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5153
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3460
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
55523
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10195
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
10042
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
16845
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
3165
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.