Listen-to xml parsing example thingie

well, i got bored one day and tried some xml parsing based on some examples i found on the web for Listen-to. don’t know if anyone is interested. Prolly not very efficient but hey. I basically just made it output an image (which you can already do) but you could do anything you want with the data. comments are good :slight_smile:


<?php
$file= "http://www.listen-to.com/xml.php/trashed20";
$depth = array();
$data1=array();


function startElement($parser, $name, $attrs) {
global $name;
    global $depth;
    for ($i = 0; $i < $depth$parser]; $i++) {}
    $depth$parser]++;
}

function endElement($parser, $name) {
    global $depth;
    $depth$parser]--;
}

function characterData($parser, $data) {
global $data1;
if ($data!="	"){$data1] =$data;}}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);

header ("Content-type: image/jpeg");
$im = @imagecreatefromjpeg ("temp2.jpg"); 
$tc  = imagecolorallocate ($im, 255, 255, 255);
$time=date("m/d/y g:i a",$data1[11]);
$track=$data1[7];
$group=$data1[3];
imagestring ($im, 3, 230, 7, $time, black);
imagestring ($im, 3, 126, 31, $group, black);
imagestring ($im, 3, 136, 52, $track, black);
imagejpeg ($im,"",85);
?>




very cool.

I’m doing some client work now using “web services” (buzzword) and SOAP to integrate an inventory database with various platforms.

On my team’s homepage, XML was used to dynamically feed info to the news system from a simple Access db (Flash and static pages) with ASP… not PHP. http://www.roboraiders.com

How long have you been working with PHP?

i’ve been working with php since may of last year. I wrote our whole website ( http://www.lightningrobotics.com ) in php and using mysql. Making xml files to parse for every time someone wants to access your news is prolly pretty hard on your system. Doesn’t ASP have dirrect connections to access db’s?

I chose php as it is c++ structured and syled which i like :slight_smile:

Cool

A connection is made to an Access db just like you would do for any other ODBC connection. The server we’re using is a 1.4 Ghz P3 Dell, so it doesn’t seem to get bogged down too often ;-).

If you are interested, this is the code to the asp script requested by the Flash intro to get the latest news:


var months = new Array ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
var monthValue = new Array (13,14,15,16,17,18,19,20,21,22,23,24);
var now = new Date();
var month = now.getMonth();
var date = now.getDate();
var day = now.getDay();
var year = now.getYear();
var week = 1;
if (year < 2000) year = year + 1900;
var monarr = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) monarr[1] = 29;
sday = day;
while (date != 0){
date --;
sday --;
	if (sday < 0){
		sday = sday + 7;
		week ++;
	}
}
var currentMonth = months[month]
var currentWeek = week
var current_category = monthValue[month]
var out
sql = "SELECT * FROM data where data_category = " + current_category + " order by data_subcategory, data_name";
rs = Server.CreateObject("ADODB.RecordSet");
rs.Open (sql, Conn, 2,3);
if (rs.EOF){ 	// check to see if rs is empty
out += ('	<button name="No articles">
')
out += ('		<info>There are no new events for this week.</info>
')
out += ('	</button>
')
}
else{			// rs not empty

	temp_subcat= ""; //this string used to group records by subcategory
	fields = new Object(); // this object used to store fields in records

while (! (rs.EOF)){

	// clean up data and put it into the fields object:
	for ( field_number = 0; field_number < rs.fields.count ; field_number++ )
		{
		temp_field = String(rs.fields(field_number).Name);
		temp_content = String(rs.fields.item(temp_field));
		temp_content = temp_content.replace(/'/g,"'");
		if (temp_field.search(/date/) > 0 ) 	// check for date fields
			{temp_content = slash_date(temp_content);} 
		else if (temp_content == "null") {temp_content = ""; }
		fields[temp_field] = temp_content
		} // end looping through fields in current record

		// subcategories: when we get to a new one, display it
		if (temp_subcat != fields.data_subcategory){
			temp_subcat = fields.data_subcategory;
		}
if (temp_subcat == currentWeek){
			out += ('   <data>
')
			out += ('	<name>'+fields.data_name+'</name>
')
			out += ('	<info>'+fields.data_url+'</info>
')
			out += ('	<id>'+fields.data_uid+'</id>
')
			out += ('   </data>
')
		} else {
}

		rs.move(1);
		} // end while recordset
	 // end else (rs not empty)
rs.Close ();
}
if (out==""){
			out += ('   <data>
')
			out += ('	<name>No articles.</name>
')
			out += ('	<info>There are no new events for this week.</info>
')
			out += ('	<id>undefined</id>
')
			out += ('   </data>
')
}
Response.Write(out)
Conn.Close ();