473,624 Members | 2,408 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($p arser, $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($par ser, $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($parse r,$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>Breakfas t</title>
<end>0915</end>

<infourl>http ://www.bbc.co.uk/cgi-perl/whatson/prog_parse.cgi? FILENAME=200501 07/20050107_0600_4 223_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 1942
ro***********@g mail.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($p arser, $name, $attrs) {
global $order, $index, $currentElement , $temp;

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

function endElement($par ser, $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($c urrentElement, $order)) {
if (isset($temp[$index])) {
$temp[$index] .= $data;
}
}
}

?>
JW

Jul 17 '05 #2
<ro***********@ gmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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($par ser, $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
2776
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 print_r cmd any suggestions would be great. Thanks much Todd //avShed array Array ( => Array ( => 1 => 08:00 ) => Array ( => 1 => 08:05 ) => Array ( => 1 => 08:10 ) => Array ( => 1 => 08:15 ) => Array ( => 1 => 08:20 ) => Array...
15
5173
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
3472
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( "name", "title", "reports to user id", "start date in the format: mm/dd/yyyy" ) ); How can I display this hierarchy in simple nested <li> tags in the most
12
55544
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 also removed) So far I have (val is value, ar is array, returns new array):
8
10213
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 it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1 = new Array(); GridArrayName1 = new Array ('test-value'); GridArrayName1 = GridArrayName1...
58
10114
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 code... TCHAR myArray; DoStuff(myArray);
104
16922
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 that array Could you show me a little example how to do this? Thanks.
7
3189
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 up looking for characters that wrap around the stripped characters and it ends up as a recursive ordeal that fails to identify a poorly constructed $_GET variable (when someone hand-types the item into the line and makes a simple typing error).
17
7237
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 to show the array data to the end user. Can I do that? How?
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6111
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2610
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 we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.