Hi- I have a question regarding XML parsing with PHP. I'm doing a music_review.php which is going to parse music reviews, which are stored via PHP. I've already got the parser, but I don't know how to make it look only at a specific review.
<?php
$xml_name = $get
// HOW TO GET THIS TO PICK THE RIGHT XML FILE? XML NAME STORED IN $GET
$xml_file = "review.xml";
$xml_headline_key = "*REVIEW*CONTENT*HEADLINE";
$xml_review_key = "*REVIEW*CONTENT*REVIEW";
$xml_tracklist_key = "*REVIEW*CONTENT*TRACKLIST";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description, $tracklist;
}
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, $xml_tracklsit_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_review_key:
$story_array[$counter]->review = $data;
$counter++;
break;
case $xml_tracklist_key:
$story_array[$counter]->tracklist = $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, filesize($xml_file)) 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);
?>
And then later on in the page, buried in the layout of HTML, which I'll omit...
<div id="content"><p><?php
for($x=0;$x<count($story_array);$x++){
echo "\t<p>" . $story_array[$x]->headline . "</p>\n";
echo "\t\t\n";
echo "\t<p>" . $story_array[$x]->description . "</p>\n";
}
?>
This would work great, except I only want to take out ONE entry at a time. How to do this? Anybody? Please help!