MarkMan5 Posted November 13, 2006 Share Posted November 13, 2006 I have modified a free third party php script to display an xml feed. What I now need to do is display the date, which is an attribute of an element. <FS NAME="date" VALUE="2006-05-11" /> How do I display the date? The code is below. Thanks in advance![pre]<?//$headline_style = 'news';$headline_style = '';$description_style = '';$feed_url = '';$show_detail = false;$insideitem = false;$tag = "";$title = "";$date = "";$description = "";$link = "";$image = "";$insideimage = false;$max = 3;$count = 0;function render_news($feed_url, $showdetail, $headlinestyle, $detailstyle) { global $show_detail, $headline_style, $detail_style, $max, $count, $insideitem, $insideimage; $insideitem=false; $insideimage=false; $count = 0; $show_detail = $showdetail; $headline_style = $headlinestyle; $detail_style = $detailstyle; $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $fp = @fopen($feed_url,"r") or die("Error reading xml data."); if ($fp) { 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); } else { echo '<span class="'. $detail_style .'">Feed is temporarily not available</span>'; } // Free up memory used by the XML parser xml_parser_free($xml_parser);}function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link, $date, $image, $insideimage; if ($insideitem || $insideimage) { $tag = $name; } if ($name == "R" ) { $insideitem = true; } if ($name == "IMAGE") { $insideimage = true; }}function endElement($parser, $name) { global $insideitem, $tag, $title, $description, $link, $image, $date, $insideimage, $show_detail, $headline_style, $detail_style, $count, $max; if ($name == "do not use....") { echo '<img src="'. htmlspecialchars(trim($image)) .'" /><br /><br />'; $insideimage=false; $image=""; } else if ($name == "R") { //&& $count < $max) { //$count++; printf('<a href="%s" class="'. $headline_style .'" target="_blank"><b>%s</b></a><br />',trim($link),trim($title)); if ($show_detail) printf('<span class="'. $detail_style .'">%s</span><br />%s<br />',trim($description), trim($date)); else { echo "<br /><br />"; } $title = ""; $description = ""; $date = ""; $link = ""; $insideitem = false; } else if ($count >= $max) { $title = ""; $description = ""; $link = ""; $insideitem = false; }}function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link, $date, $image, $insideimage; if ($insideimage) { switch ($tag) { case "U": $image .= $data; break; } } if ($insideitem ) { switch ($tag) { case "T": $title .= $data; break; case "S": $description .= $data; break; case "FS": $date .= $data; break; case "U": if (!is_string($link)) $link=""; $link .= $data; break; } }}?>[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/27141-how-to-read-xml-element-attribute/ Share on other sites More sharing options...
MarkMan5 Posted November 15, 2006 Author Share Posted November 15, 2006 For those interested, the solution is this:Add global variable $date1 to beginning of script and to lists of global variables in all functions. Also change the variable in the endElement() function to be $date1 instead of $date . See bold below for final change:function startElement($parser, $name, $attrs) { global $insideitem, $tag, $title, $description, $link, $date, [b]$date1[/b], $image, $insideimage; [b]if ($insideitem || $insideimage) { if ($name == "FS") {$date1 = $attrs["VALUE"];} else {$tag = $name;} } [/b] if ($name == "R" ) { $insideitem = true; } if ($name == "IMAGE") { $insideimage = true; }} Quote Link to comment https://forums.phpfreaks.com/topic/27141-how-to-read-xml-element-attribute/#findComment-124786 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.