I am attempting to show a status of a server for a friend of mine (I have webspace, he does not). I am using the following XML page template to pull data:
http://www.camelotherald.com/xml/servers.xml
I am trying to show ALL server information for the "galahad" server, and I have the following code pulling some of the info, but cannot get it to properly pull all of it. Any help would be VERY appreciated, as I am new to XML and just starting to learn it.
<?php
print "<TABLE BORDER=1 WIDTH=75%>";
$insideitem = false;
$tag = "";
$da_server = "";
$da_population = "";
$da_type = "";
$da_status = "";
$da_totalpop=0;
function startElement($parser, $tagName, $attrs) {
global $insideitem, $tag, $da_server, $da_type;
global $relic_name, $da_owner, $da_rtype, $da_realm;
if ($insideitem) {
$tag = $tagName;
}
elseif ($tagName == "SERVER")
{
$insideitem = true;
while (list ($key, $val) = each ($attrs)) {
switch($key) {
case "NAME": $da_server=$val;break;
case "TYPE": $da_type=$val;break;
} // end case
} // end while
}
if ($tagName == "RELIC")
{
$insideitem = true;
while (list ($key, $val) = each ($attrs)) {
switch($key) {
case "NAME": $relic_name=$val;break;
case "TYPE": $da_rtype=$val;break;
} // end case
} // end while
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $da_server, $da_population, $da_type, $da_status,$da_totalpop, $printdata;
global $relic_name, $da_owner, $da_rtype, $da_realm;
if ($insideitem) {
switch ($tag) {
case "POPULATION": $da_population .= $data; $da_totalpop += $data; break;
case "STATUS": $da_status .= $data; break;
}
}
}
function endElement($parser, $tagName) {
global $insideitem, $tag, $da_server, $da_population, $da_type, $da_status, $printdata;
global $relic_name, $da_owner, $da_rtype, $da_realm;
if ($tagName == "SERVER") {
if($da_server == "Galahad") {
$printdata = "<tr><td colspan=3><font face=arial size=+1><b>$da_server Server Stats</b></td></tr>";
$printdata .= "<tr><td width=25> </td><td><font face=arial size=-1><b>Status:</b></font> <Font face=arial size=-1 color=red> $da_status</font></td><td><font face=arial size=-1><b>Population</b> $da_population</td></tr>";
if($relic_name) {
$printdata .= "<tr><td><font face=arial size=-1><b>Relic Name:</b>$relic_name (<i>$da_rtype</i>)</font></td><td><font face=arial size=-1><b>Realm</b> $da_realm</font></td><td><font face=arial size=-1><b>Current Owner:</b> $da_owner
</font></td></tr>";
}
}
$da_server = "";
$da_population = "";
$da_status = "";
$da_type = "";
$insideitem = false;
$da_owner = "";
$da_realm = "";
$da_rtype = "";
$insideitem = false;
}
}
// Create an XML parser
$xml_parser = xml_parser_create();
// Set the functions to handle opening and closing tags
xml_set_element_handler($xml_parser, "startElement", "endElement");
// Set the function to handle blocks of character data
xml_set_character_data_handler($xml_parser, "characterData");
// Open the XML file for reading
$fp = fopen("http://www.camelotherald.com/xml/servers.xml","r") or die("Error reading RSS data.");
// Read the XML file 4KB at a time
while ($data = fread($fp, 4096))
// Parse each 4KB chunk with the XML parser created above
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)));
// Close the XML filef
fclose($fp);
print "$printdata";
print "</TABLE>n";
// Free up memory used by the XML parser
xml_parser_free($xml_parser);
// end of file
?>