phenam
07-30-2004, 11:25 PM
Hi,
First off I am a newbie to this and I'm trying my best to learn. Anyhow, I'm trying to parse this XML file (http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wpa/MRSS/topalbums/sf=143441/genre=0000800/limit=10/rss.xml). Though I'm running into a problem. This tag <itms:coverArt> appears three times in the same <item> with different attributes. I'm trying to figure out how I can choose the one I want to use. Does anyone know a way to filter out the attributes you don't want to use? Any help would be much appreciated. Here's the code I'm using to display all of them.
$insideitem = false;
$tag = "";
$cover = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $cover;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $cover;
if ($name == "ITEM") {
echo $cover;
$cover = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $cover;
if ($insideitem) {
switch ($tag) {
case "ITMS:COVERART":
$cover .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wpa/MRSS/topalbums/sf=143441/genre=0000800/limit=10/rss.xml","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
First off I am a newbie to this and I'm trying my best to learn. Anyhow, I'm trying to parse this XML file (http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wpa/MRSS/topalbums/sf=143441/genre=0000800/limit=10/rss.xml). Though I'm running into a problem. This tag <itms:coverArt> appears three times in the same <item> with different attributes. I'm trying to figure out how I can choose the one I want to use. Does anyone know a way to filter out the attributes you don't want to use? Any help would be much appreciated. Here's the code I'm using to display all of them.
$insideitem = false;
$tag = "";
$cover = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $cover;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $cover;
if ($name == "ITEM") {
echo $cover;
$cover = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $cover;
if ($insideitem) {
switch ($tag) {
case "ITMS:COVERART":
$cover .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wpa/MRSS/topalbums/sf=143441/genre=0000800/limit=10/rss.xml","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);