Page 1 of 1

XML Parser

Posted: Wed Jun 01, 2005 7:45 pm
by Ross2376
In an XML parser, if I set a variable equal to some charData from the XML (and globalize the variable), will I still be able to access the variable after the parser is done so to speak (after the parser code)?

Posted: Wed Jun 01, 2005 11:19 pm
by infolock
if you use include then yes. otherwise, no. global only makes the variable global within the file scope.

Posted: Wed Jun 01, 2005 11:57 pm
by Ross2376
Oh, so after the parser is done, it would eliminate all variables with it? I'm a bit confused on the include. What would I include in my file? The parser code?

Posted: Thu Jun 02, 2005 5:03 pm
by Ross2376
^^^ bump

Posted: Thu Jun 02, 2005 11:58 pm
by Ross2376
Ok, obviously no one is looking at this or you cant figure out wtf I'm trying to do, therefore, I will post my code. View the output at http://69.57.144.90/~aod/template.php?page=7

The XML feed is at http://www.teamwarfare.com/xml/viewteam ... estruction

If you look at line 69, that echo statement works. It will print the rank, currently 12. The if statement below it checks to see if its on the last XML element. If so, it changes $end to true. That activates the next if statement which prints the table. I use that $end variable to have the script wait until the end of all elements (so i dont have 2034803820483 tables) to print the table. It also prevents the table from displaying before it has been through all the required information to echo (map name...players..etc). Now, what I don't understand is this:

If its at the end of the elements, it should have assigned all the values already. They are global so that means they should work. The values don't display in that long echo. Why don't they? I appreciate your efforts.

Code: Select all

function startElement ($parser, $element, $attributes)
		{
			global $ladder_name, $match_status, $map_name, $elementName, $player_name,
					$inside_links, $elementCount;
			
			$inside_links = false;
			$player_name = array ();
			$elementName = $element;
			
			while (list ($key, $value) = each ($attributes))  {
				if (($element == "LADDER") && ($key == "NAME"))  {
					$ladder_name = $value;
				} elseif (($element == "MATCH") && ($key == "STATUS"))  {
					$match_status = $value;
				} elseif (($element == "MAP") && ($key == "NAME"))  {
					$map_name = $value;
				} elseif (($element == "PLAYER") && ($key == "NAME"))  {
					$player_name[] = $value;
				} elseif ($element == "LINKS")  {
					$inside_links = true;
				}
			}
			
		}
	
	function charData ($parser, $data)
		{
			global $elementName, $inside_links, $rank, $wins, $losses, $forfeits, $opponent,
					$match_date, $side, $status, $opponent_link, $elementCount;
			switch ($elementName)  {
				case "RANK":
					$rank = $data;
					break;
				case "WINS":
					$wins = $data;
					break;
				case "LOSSES":
					$losses = $data;
					break;
				case "FORFEITS":
					$forfeits = $data;
					break;
				case "STATUS":
					$status = $data;
					break;
				case "OPPONENTNAME":
					$opponent = $data;
					break;
				case "MATCHDATE":
					$match_date = $data;
					break;
				case "SIDESELECTION":
					$side = $data;
					break;
				case "HTMLLINK":
					if (!$inside_links)  {
						$opponent_link = $data;
					}
					break;
			}
		}
	
	function endElement ($parser, $element)
		{
			global $authorized, $rank, $wins, $losses, $status, $match_date, $match_status,
					$opponent_link, $opponent, $side, $forfeits, $map_name, $ladder_name,
					$player_name;
					
			echo $rank;
			if ($element == "TEAM")  {
				$end = true;
			}

			if ($end) {
				echo ("<div align=\"left\">
						<table border=\"0\" width=\"100%\">
							<tr>
								<td colspan=\"5\">
									<p align=\"left\"><font size=\"2\"><strong>$ladder_name</strong></font></p><font size=\"2\">
								</td>
							</tr>
							<tr>
								<td width=\"100\"><font size=\"2\">Rank:</font></td>
								<td><font size=\"2\">$rank</font></td>
								<td width=\"65\"><font size=\"2\"> </font></td>
								<td width=\"100\"><font size=\"2\">Next Match:</font></td>
								<td><font size=\"2\">$match_date</font></td>
							</tr>
							<tr>
								<td><font size=\"2\">Wins:</font></td>
								<td><font size=\"2\">$wins</font></td>
								<td><font size=\"2\"> </font></td>
								<td><font size=\"2\">Opponent:</font></td>
								<td><font size=\"2\"><a href=\"$opponent_link\">$opponent</a></font></td>
							</tr>
							<tr>
								<td><font size=\"2\">Losses:</font></td>
								<td><font size=\"2\">$losses</font></td>
								<td><font size=\"2\"> </font></td>
								<td><font size=\"2\">Map:</font></td>
								<td><font size=\"2\">$map_name</font></td>
							</tr>
							<tr>
								<td><font size=\"2\">Forfeits:</font></td>
								<td><font size=\"2\">$forfeits</font></td>
								<td><font size=\"2\"> </font></td>
								<td><font size=\"2\">AoD Side:</font></td>
								<td><font size=\"2\">$side</font></td>
							</tr>
							<tr>
								<td><font size=\"2\">Team Status:</td>
								<td><font size=\"2\">$status</td>
								<td><font size=\"2\"> </font></td>
								<td><font size=\"2\">Team Roster:</font></td>
								<td><font size=\"2\">");
								foreach ($player_name as $key => $value)  {
									if (count ($player_name) != ($key + 1))  {
										echo ("$value, ");
									} else  {
										echo ("$value");
									}
								} 
								echo ("</font></td></tr></table></div></span></td></tr></table></center></div>");
			}
		}
		
	$parser = xml_parser_create();
	xml_set_element_handler ($parser, "startElement", "endElement");
	xml_set_character_data_handler ($parser, "charData");
	$xml_feed = fopen ("http://www.teamwarfare.com/xml/viewteam_v2.asp?team=Area+of+Destruction", "r");
		
	while ($xml_chunk = fread ($xml_feed, 4096))  {
		xml_parse ($parser, $xml_chunk, feof ($xml_feed));
	}
	
						
	fclose ($xml_feed);
	xml_parser_free ($parser);

Posted: Fri Jun 03, 2005 3:07 am
by timvw
The easiest to debug this is the following:

everywhere you assign a value to a variable:

Code: Select all

echo "variableX recieves: $value <br>";
everywhere you want to output a value:

Code: Select all

echo "variableX has: $variableX<br>";

This way you can see relatively fast where things don't go as you would expect...

Posted: Fri Jun 03, 2005 1:34 pm
by Ross2376
Yes and I have done this. Line 69 is a debugging line. But I cannot figure out why the big echo table will not spit out the values (or maybe it is and they are just blank). The variable works just fine before the echo statement, so the value is there. I figure that the rank on 69 is being echoed quite a few times, because the handler is called many times. It only displays once though because rank is only one tag. Wow, I just realized that rank is loosing its value right after that XML element is over. I updated line 69 to:

Code: Select all

echo (" bla " . $rank);
The output is at http://69.57.144.90/~aod/template.php?page=10. You can see that the value is lost after one iteration through the handlers with that element (rank). I would imagine that is why the variables at the end are empty. Does anyone know why it does this or how to fix it?

Posted: Mon Jun 06, 2005 11:40 am
by Ross2376
Anyone going to respond?