[thelist] xml_parse PHP

Mark Joslyn Mark.Joslyn at SolimarSystems.com
Tue Nov 1 15:54:10 CST 2005


Can anyone tell me why the if (in_array("joe", $access)) is blowing my
xml_parse up? If I remove the if statement, the XML parses fine - going
crazy here.... code is below.

Any help would be greatly appreciated.

markJ


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>xml parse test</title>
</head>

<body>
<table>
<?
$access = array('joe');

if (in_array("joe", $access)) {

	$xml_parser = xml_parser_create();
	xml_set_element_handler($xml_parser, "startElement", "endElement");

	xml_set_character_data_handler($xml_parser, "characterData");
	xml_set_default_handler($xml_parser, "characterData");
	
	$fp = fopen("myFile.xml","r");
	
	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);

	$insideitem = false; 
	$tag = ""; 
	$title = ""; 
	$description = "";
	$pubDate = "";
	$category = "";
	$link = "";
	$mCount = 0;

	function startElement($parser, $tagName, $attrs) { 
		global $insideitem, $tag; 
		if ($insideitem) {
			$tag = $tagName;
		} elseif ($tagName == "ITEM") { 
	 		$insideitem = true; 
		} 
	}

	function characterData($parser, $data) { 
		global $insideitem, $tag, $title, $description, $pubDate,
$category, $link, $mCount;
		if ($insideitem) {
			switch ($tag) { 
	           	case "TITLE": 
	           		$title .= $data; 
	           	break; 
	           	case "DESCRIPTION": 
	           		$description .= $data; 
	           	break;
			   	case "PUBDATE": 
	           		$pubDate .= $data;
			   	break;
			   	case "CATEGORY": 
	           		$category .= $data;
	           	break;
	           	case "LINK": 
	           		$link .= $data; 
	           	break; 
 			} 
		} 
	}

	function endElement($parser, $tagName) { 
		global $insideitem, $tag, $title, $description, $pubDate,
$category, $link, $mCount; 
   		if ($tagName == "ITEM") {
			if ($mCount < 4) {
?>
	<tr>
		<td align="left" valign="top"><a href="<? echo trim($link);
?>" target="_blank"><? echo trim($title); ?></a></td>
	</tr>
<?
			}
    		$title = ""; 
  			$description = "";
			$pubDate = "";
			$category = "";
  			$link = "";
			$insideitem = false;
			$mCount = $mCount + 1;
		}
	}
}
?>
</table>
</body>
</html>




More information about the thelist mailing list