Jump to content

Parsing xml file's, WITH php


Ninjakreborn

Recommended Posts

[code]<?php
// Build an array with all the channel names.
$channels = array ("I11187",
  "I11829",
  "I11269",
  "I11768",
  "I11993",
  "I19556",
  "I11490",
  "I11479",
  "I11959",
  "I23319",
  "I18774",
  "I11287",
  "I12553",
  "I24634",
  "I23324",
  "I22564",
  "I23327",
  "I23328",
  "I23329",
  "I10240",
  "I10244",
  "I10241",
  "I18429",
  "I16585",
  "I24553",
  "I10243",
  "I10021",
  "I12852",
  "I16374",
  "I10142",
  "I10145",
  "I10139",
  "I10161",
  "I10162",
  "I10035",
  "I10057",
  "I10051",
  "I10153",
  "I11097",
  "I11163",
  "I11164",
  "I11207",
  "I10149",
  "I10989",
  "I14321",
  "I14909",
  "I10918",
  "I16409",
  "I45654",
  "I10179",
  "I12444",
  "I16485",
  "I12410",
  "I14899",
  "I14776",
  "I16011",
  "I20789",
  "I10986",
  "I16361",
  "I10138",
  "I11218",
  "I44228",
  "I11180",
  "I16331",
  "I11150",
  "I16615",
  "I18327",
  "I16617",
  "I16618",
  "I18284",
  "I16616",
  "I18544",
  "I18179",
  "I11158",
  "I14902",
  "I14771",
  "I12574",
  "I26784",
  "I10093",
  "I12131",
  "I10171",
  "I11006",
  "I16123",
  "I30103",
  "I21450",
  "I19059",
  "I18332",
  "I25354",
  "I31634",
  "I19283",
  "I18644",
  "I21852",
  "I21063",
  "I24935",
  "I24248",
  "I29323",
  "I28452",
  "I12324",
  "I16345",
  "I10239",
  "I11118",
  "I15377",
  "I27773",
  "I19247",
  "I18715",
  "I19979",
  "I25238");
  // Build the file name's dynamically based on the above file names.
// register empty array to hold the upcoming information
$files = array();
// run through all the channels, assining there full file names into an array
foreach ($channels as $k) {
$files[] = $k . ".labs.zap2it.com.xml"; // assign each file name into the files array
}
// attempt to create an xml parser
if (!$parser = xml_parser_create()) {
exit("There was a problem creating the parser");
}
// Functions To go into the element handlers, later on
// variables to pass to user functions
$currentdate = date();
$showstarttime = "";
$showstoptime = "";
$tag = "<programme></programme>";
$attributes = "@start, @stop, <title>, </title>";
// function to start searching
function starttag($parser, $tag, $attributes) {

}
// function to end searching
function endtag($parser, $name) {

}
// function to take data from tags
function tagdata($parser, $data) {

}
xml_set_element_handler($parser, starttag(), endtag());
xml_set_character_data_handler($xmlparser, tagdata());

// use the file array created earlier to get to work
// create an array to hold information on all the currently playing shows
// from all the xml file's
$nowplaying = array();
foreach ($files as $k => $v) { // run through all the file's
if (!$open = fopen($v, "r")) { // opening them
// we can't do anything if any file's didn't open, because we don't have all the
// necessary data, so kill the script
exit("File could not be opened");
}
while ($data = fread($open, 4096)){
  if (!xml_parse($xmlparser, $data, feof($open))) {
      $reason = xml_error_string(xml_get_error_code($xmlparser));
      $reason .= xml_get_current_line_number($xmlparser);
      die($reason);
  }
}

}// close foreach
?>[/code]
Ok, I went through about 8 tutorials, and got somethign together.

I worked this up, and ran into a lot of error's.  I checked some references, and did some fixes, and removed a lot of the errors.

"I have another post open on misc about the date, I have to work that in after I get it doing the basics".
I went over the tutorial on w3schools, and other places about xml/xsl and after thinking, because of WHAT I am wanting to do here, it's best to parse the file's with php, and return the results.
since I have to get an updated copy everytime someone comes.

Each channel is in a sepearet page, here is some example coding, that a channel might contain

[code]  <programme start="20070129140000 -0500" stop="20070129150000 -0500" channel="I44228.labs.zap2it.com">
    <title lang="en">mtvU Video Hour</title>
    <category lang="en">Music</category>
    <category lang="en">Series</category>
    <episode-num system="dd_progid">SH684893.0000</episode-num>
  </programme>
  <programme start="20070129150000 -0500" stop="20070129160000 -0500" channel="I44228.labs.zap2it.com">
    <title lang="en">mtvU Video Hour</title>
    <category lang="en">Music</category>
    <category lang="en">Series</category>
    <episode-num system="dd_progid">SH684893.0000</episode-num>
  </programme>
  <programme start="20070129160000 -0500" stop="20070129170000 -0500" channel="I44228.labs.zap2it.com">
    <title lang="en">BFOC Student Film of the Week</title>
    <sub-title lang="en">Urban Shogun</sub-title>
    <date>20070126</date>
    <category lang="en">Art</category>
    <category lang="en">Limited Series</category>
    <episode-num system="dd_progid">EP895481.0001</episode-num>
    <episode-num system="onscreen">101</episode-num>
  </programme>[/code]
I have to look at each file.  Inside each file I have to look at all the contents of each pair of
<programme> tag's.  I have to check the start time and stop time doing this calculation
if the start time is less than the current time, AND the stop time is greater than the current time, then pull the title out, and save it in an array (I guess I will call title).

I need it to do that, for all of them, so I can get a list of 1 programme (the one playing now) for each and every channel.

I know my current script has a long way's to go.

However when I first did it, I ran into many errors.
I am down to there.


[quote]Warning: Wrong parameter count for date() in /home/wiltm/www/test/nowplaying.php on line 123

Warning: Missing argument 1 for starttag() in /home/wiltm/www/test/nowplaying.php on line 129

Warning: Missing argument 2 for starttag() in /home/wiltm/www/test/nowplaying.php on line 129

Warning: Missing argument 3 for starttag() in /home/wiltm/www/test/nowplaying.php on line 129

Warning: Missing argument 1 for endtag() in /home/wiltm/www/test/nowplaying.php on line 133

Warning: Missing argument 2 for endtag() in /home/wiltm/www/test/nowplaying.php on line 133

Warning: Missing argument 1 for tagdata() in /home/wiltm/www/test/nowplaying.php on line 137

Warning: Missing argument 2 for tagdata() in /home/wiltm/www/test/nowplaying.php on line 137

Warning: xml_set_character_data_handler(): supplied argument is not a valid XML Parser resource in /home/wiltm/www/test/nowplaying.php on line 142

Warning: xml_parse(): supplied argument is not a valid XML Parser resource in /home/wiltm/www/test/nowplaying.php on line 155

Warning: xml_get_error_code(): supplied argument is not a valid XML Parser resource in /home/wiltm/www/test/nowplaying.php on line 156

Warning: xml_get_current_line_number(): supplied argument is not a valid XML Parser resource in /home/wiltm/www/test/nowplaying.php on line 157[/quote]

I have done a lot of legwork, before bringing this here, I didn't want to just come here, and dump a bunch of problems, so I ahve fixed what I can.  Tutorials are good, I am about to also sign up (today) for the orielly bookshelve again.  IN hte meantime I have done all the tutorials I could find, referencing the manual, as much as I could. 

Is there another tutorial I could look over (or 2) I may not have used, so I can try and refine my code more.

Or pointers on how to get this going in the right direction for what I am going to do.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.