tags:

views:

368

answers:

3

I have an xml feed at this url

Now im trying parse the content, particularly the content in <REDIRECT></REDIRECT> tags. I use the following code to try and parse the content but it isnt working and im not sure what im doing wrong.

$xml_file = $ADurl;

$xml_headline_key = "*XML*RESULTS*LISTING*REDIRECT";
$xml_description_key = "*XML*RESULTS*LISTING*REDIRECT";

$story_array = array();

$counter = 0;
class xml_story{
    var $headline, $description;
}

function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
}

function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
}

function contents($parser, $data){
    global $current_tag, $xml_headline_key, $xml_description_key, $counter, $story_array;
    switch($current_tag){
        case $xml_headline_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->headline = $data;
            break;
        case $xml_description_key:
            $story_array[$counter]->description = $data;
            $counter++;
            break;
    }
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($xml_file, "r") or die("Could not open file");

$data = fread($fp, 1024) or die("Could not read file");

if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);
+3  A: 

Is there a reason you can't use simplexml?

$xml = simplexml_load_file('http://www.supashare.net/test.xml'); 
$result = $xml->xpath('/XML/RESULTS/LISTING/REDIRECT');
echo $result[0];
Galen
And the same with this, it doesnt work with remote xml files
Imran
Turn on errors see what happens. Your server may not have allow_url_fopen enabled, in which case you would have to edit your php.ini (if possible) to enable it.
Galen
if simplexml_load_file won't work then replace it with this....$xml = simplexml_load_string(file_get_contents($url)); where $url is the url you want
ChronoFish
(and if "file_get_contents" won't retrieve the remote file, then you'll have to check your INI file....) -- But regardless you should be able to get the remote contents and shove into a string - and then into simplexml_load_string...
ChronoFish
file_get_contents($url) works fine by itself but it doesnt load within simplexml
Imran
if file_get_contents works, then you just pass that string to simplexml_load_string() instead, and it will work. Not sure why one would work and not the other though, im pretty sure they both use the fopen wrapper.
Galen
When using file_get_contents(), the whole xml file isnt read, only a few bits of text within the file is. I think that is why file_get_contents() works but not within simplexml
Imran
So any other ideas?
Imran
+2  A: 
$xml = stream_get_contents($fp);
$xml = new SimpleXMLElement($xml);
echo $xml->RESULTS->LISTING->REDIRECT;
David Barnes
Well the problem is that it cant parse remote xml files.
Imran
What do you mean? Do you have the XML file locally on your server? If so, change the URL to that filename and it will work.
David Barnes
Nope, the xml file is on another server on another server.
Imran
EDIT: I have changed it to use the $fp you defined in your code. Will this work?
David Barnes
Didnt work either :S
Imran
+1  A: 
$doc = new DomDocument();
$doc->load('http://www.supashare.net/test.xml');
$q = new DomXPath($doc);
echo $q->query('//REDIRECT')->item(0)->nodeValue;
troelskn
I remember you from sitepoint, you were a guy whose posts i loved to read. Can you tell me why you chose domdocument over simplexml?
Galen
Hmm.. I think your confusing me with something else :SAs to your answer.. its not working with remote XML files either
Imran
They use the same underlying parse (libxml), so it's just syntax. Personally I like DOM better because it's a standard api. It's just a preference.
troelskn