Forum Moderators: coopster

Message Too Old, No Replies

Get namespaces with php

         

ProuteurFou

3:18 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



Hi
I'm using the SimpleXMLElement class to get some info of an rss feed
The thing is I get nothing about the info included in the namespaces (<media:title> <media:content> <media:credit>) when I write
print_r($xml)

For example in this code, what should I do to get the info in the media node:

<media:player url="http://youtube.com/?v=hjlQfMECSdA"/>
<media:thumbnail url="http://sjl-static10.sjl.youtube.com/vi/hjlQfMECSdA/2.jpg" width="120" height="90"/>

thanks!

Stuperfied

3:02 am on Dec 10, 2006 (gmt 0)

10+ Year Member



I have something that might be of some use to you in this area. The xml_parser.inc code is probably just what you need, however the stack_sorter.inc code is only half of the rest of what you need but could be usefull in providing you with the necessary examples you require to fashion your own script. I am interested in viewing the finished product, please post your finished script and your ideas. Also please PM me to alert me to your post so as to ensure that I dont miss it.

File: xml_parser.inc

<?php

//////////////////////////////////////////////////////////////
// //
//XML Parser (PHP 4, PHP 5) //
// //
// -------------------------------------------------------- //
// //
// Parameters: //
// $xmllink //
// //
// Accepted Arguments: //
// $xmllink = "example_records/example_page_data.xml"; //
//parsexml($xmllink); //
// //
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// Note: This code was written by me (Mr Michael Hurt) utilizing //
// examples found on the official PHP website and the aid of //
// expertise provided by members of webmasterworld.com. The use of //
// this code is only permitted so long as this notice remains intact //
// and is not removed or altered. This code is concidered community //
// software and cannot be licenced or restricted in any way shape or //
// form. //
//////////////////////////////////////////////////////////////

// Create Stack

function startTag($parser, $name, $attrs) {
global $stack;

$tag = array("name" => $name, "attrs" => $attrs);
array_push($stack, $tag);
}

function cdata($parser, $cdata) {
global $stack;

$stack[count($stack)-1]['cdata'] .= $cdata;
}

function endTag($parser, $name) {
global $stack;

$stack[count($stack)-2]['children'][] = $stack[count($stack)-1];
array_pop($stack);
}

// Parse XML

function parsexml($xmllink) {
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "cdata");

$data = xml_parse($xml_parser,file_get_contents($xmllink));
if(!$data) 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);
}

?>


File: stack_sorter.inc

<?php

//////////////////////////////////////////////////////////////
// //
// Stack Sorter (PHP 4, PHP 5) //
// XML Content Management System //
// //
// -------------------------------------------------------- //
// //
// Parameters: //
// $stack //
// //
// Accepted Arguments: //
// $container = sortStack($stack); //
// //
// Return Type: //
// Multi-Dimensional Array (index/assoc) //
// //
//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
// Note: This code was written by me (Mr Michael Hurt) utilizing //
// examples found on the official PHP website and the aid of //
// expertise provided by members of webmasterworld.com. The use of //
// this code is only permitted so long as this notice remains intact //
// and is not removed or altered. This code is concidered community //
// software and cannot be licenced or restricted in any way shape or //
// form. //
//////////////////////////////////////////////////////////////

// Sort Stack
function sortStack($stack) {

$container = array();
$count1 = 0;
$count2 = 0;
$keyTest1 = 0;
$keyTest2 = 0;
$sample = "";

// Detect Duplicate Keys
function checkKeys($sample, $testString) {
$i=0;

for ($x=0; $x<sizeof($sample); $x++) {

if ($sample[$x][name] == $testString) {
$i++;
}

}

if ($i > 1) {
return true;
}
else {
return false;
}
}

// Store Tags and Data
for($i = 0; $i < sizeof($stack[0][children]); $i++) {
$valname = $stack[0][children][$i][name];
for($x = 0; $x < sizeof($stack[0][children][$i][children]); $x++) {
$valname2 = $stack[0][children][$i][children][$x][name];

// detect duplicate keys
$sample = $stack[0][children];
$keyTest1 = checkKeys($sample, $valname);

if ($keyTest1 == true) {
$container[strtolower($valname)][$count1][$valname2] = $stack[0][children][$i][children][$x][cdata];
}
else {
$container[strtolower($valname)][$valname2] = $stack[0][children][$i][children][$x][cdata];
}

for($y = 0; $y < sizeof($stack[0][children][$i][children][$x][children]); $y++) {
$valname3 = $stack[0][children][$i][children][$x][children][$y][name];

// detect duplicate keys
$sample = $stack[0][children][$i][children];
$keyTest2 = checkKeys($sample, $valname2);

if ($keyTest2 == true) {
$container[strtolower($valname2)][$count2][$valname3] = $stack[0][children][$i][children][$x][children][$y][cdata];
}
else {
$container[strtolower($valname2)][$valname3] = $stack[0][children][$i][children][$x][children][$y][cdata];
}

}

if ($keyTest2 == true) {
$count2++;
}
}

if ($keyTest1 == true) {
$count1++;
}
}

return $container;
}

?>