Jump to content

[SOLVED] php XML parser problem


wtfsmd

Recommended Posts

When i run this code i do not get any error messages i just get a blank white screen i have been trying to figure out what the problem is but im stumped its acting like i am not pulling any info from the xml file. im fairly new to xml and i have only gotten my feet wet on parsing and dealing with outputing the data from the xml elements. any help would be appreciated thanks.

 

here is the xml file its attempting to parse: http://gamebattles.com/xbox360/call-of-duty-4/team/body-count-1/stats.xml

 

 

<html>
<head>
<title> Body Count - Call of Duty 4 </title>
</head>

<body>
<dl>
<?php

$insideitem = false;
$tag = "";
$place = "";
$previousplace = "";
$matchesplayed = "";
$wins = "";
$losses = "";
$winpercentage = "";
$gamesfor = "";
$gamesagainst = "";
$shutouts = "";
$streak = "";
$xp = "";
$level = "";
$levelup = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $place, $previousplace, $matchesplayed, $wins, $losses, $winpercentage, $gamesfor, $gamesagainst, $shutouts, $streak, $xp, $level, $levelup;
if ($insideitem) {
	$tag = $name;
} elseif ($name == "CURRENTSTATS") {
	$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $place, $previousplace, $matchesplayed, $wins, $losses, $winpercentage, $gamesfor, $gamesagainst, $shutouts, $streak, $xp, $level, $levelup;
if ($name == "CURRENTSTATS") {

	echo $place;
	echo $previousplace;
	echo $matchesplayed;
	echo $wins;
	echo $losses;
	echo $winpercentage;
	echo $gamesfor;
	echo $gamesagainst;
	echo $shutouts;
	echo $streak;
	echo $xp;
	echo $level;
	echo $levelup;



	$place = "";
	$previousplace = "";
	$matchesplayed = "";
	$wins = "";
	$losses = "";
	$winpercentage = "";
	$gamesfor = "";
	$gamesagainst = "";
	$shutouts = "";
	$streak = "";
	$xp = "";
	$level = "";
	$levelup = "";
	$insideitem = false;
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $place, $previousplace, $matchesplayed, $wins, $losses, $winpercentage, $gamesfor, $gamesagainst, $shutouts, $streak, $xp, $level, $levelup;
if ($insideitem) {
switch ($tag) {
	case "PLACE":
	$title .= $data;
	break;
	case "PREVIOUSPLACE":
	$description .= $data;
	break;
	case "MATCHESPLAYED":
	$link .= $data;
	break;
	case "WINS":
	$title .= $data;
	break;
	case "LOSSES":
	$title .= $data;
	break;
	case "WINPERCENTAGE":
	$title .= $data;
	break;
	case "GAMESFOR":
	$title .= $data;
	break;
	case "GAMESAGAINST":
	$title .= $data;
	break;
	case "SHUTOUTS":
	$title .= $data;
	break;
	case "STREAK":
	$title .= $data;
	break;
	case "XP":
	$title .= $data;
	break;
	case "LEVEL":
	$title .= $data;
	break;
	case "LEVELUP":
	$title .= $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://gamebattles.com/xbox360/call-of-duty-4/team/body-count-1/stats.xml","r")
or die("Error reading XML 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);

?>
</dl>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/113427-solved-php-xml-parser-problem/
Share on other sites

:D i made a noob mistake sorry for wasting your time. if anyone is interested in the working code here it is it could be useful for someone. it works as intended for me. you could edit out the code and use it for your own needs i got the code off this tut: http://www.sitepoint.com/article/php-xml-parsing-rss-1-0

 

<html>
<head>
<title> Body Count - Call of Duty 4 </title>
</head>
<body>
<dl>
<?php

$insideitem = false;
$tag = "";
$place = "";
$previousplace = "";
$matchesplayed = "";
$wins = "";
$losses = "";
$winpercentage = "";
$gamesfor = "";
$gamesagainst = "";
$shutouts = "";
$streak = "";
$xp = "";
$level = "";
$levelup = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $place, $previousplace, $matchesplayed, $wins, $losses, $winpercentage, $gamesfor, $gamesagainst, $shutouts, $streak, $xp, $level, $levelup;
if ($insideitem) {
	$tag = $name;
} elseif ($name == "CURRENTSTATS") {
	$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $place, $previousplace, $matchesplayed, $wins, $losses, $winpercentage, $gamesfor, $gamesagainst, $shutouts, $streak, $xp, $level, $levelup;
if ($name == "CURRENTSTATS") {


	echo $place;
	echo $previousplace;
	echo $matchesplayed;
	echo $wins;
	echo $losses;
	echo $winpercentage;
	echo $gamesfor;
	echo $gamesagainst;
	echo $shutouts;
	echo $streak;
	echo $xp;
	echo $level;
	echo $levelup;



	$place = "";
	$previousplace = "";
	$matchesplayed = "";
	$wins = "";
	$losses = "";
	$winpercentage = "";
	$gamesfor = "";
	$gamesagainst = "";
	$shutouts = "";
	$streak = "";
	$xp = "";
	$level = "";
	$levelup = "";
	$insideitem = false;
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $place, $previousplace, $matchesplayed, $wins, $losses, $winpercentage, $gamesfor, $gamesagainst, $shutouts, $streak, $xp, $level, $levelup;
if ($insideitem) {
switch ($tag) {
	case "PLACE":
	$place .= $data;
	break;
	case "PREVIOUSPLACE":
	$previousplace .= $data;
	break;
	case "MATCHESPLAYED":
	$matchesplayed .= $data;
	break;
	case "WINS":
	$wins .= $data;
	break;
	case "LOSSES":
	$losses .= $data;
	break;
	case "WINPERCENTAGE":
	$winpercentage .= $data;
	break;
	case "GAMESFOR":
	$gamesfor .= $data;
	break;
	case "GAMESAGAINST":
	$gamesagainst .= $data;
	break;
	case "SHUTOUTS":
	$shutouts .= $data;
	break;
	case "STREAK":
	$streak .= $data;
	break;
	case "XP":
	$xp .= $data;
	break;
	case "LEVEL":
	$level .= $data;
	break;
	case "LEVELUP":
	$levelup .= $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://gamebattles.com/xbox360/call-of-duty-4/team/body-count-1/stats.xml","r")
or die("Error reading XML 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);

?>
</dl>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.