...

View Full Version : Parsing XML and attributes



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);

firepages
07-31-2004, 02:41 AM
Hi , in the element parser ..function startElement($parser, $name, $attrs) {

$attrs is an array of the elements attributes , now I do not know how you want to filter these but e.g you only want the images where height="100"



<?
} elseif ($name == "ITEM") {
if($attrs['height']=='100'){
$insideitem = true;
}
}
?>


may be along the right lines ?

phenam
07-31-2004, 03:24 PM
Well, I tried it with your code but it didn't return anything. I'll keep trying different things I guess. Thanks for the reply though!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum