Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with setting XML data up in PHP

Status
Not open for further replies.

gbice

MIS
Dec 9, 2001
1
US
I have code that is reading server information and outputting data from a separate site. The below code is built to monitor several servers - and I only want to monitor ONE server. Let's assume the server name I wanted to monitor was "bobsled" - how would I pare down the below PHP info to read only information from the "bobsled" server?

Thanks! New to XML.

<?php

$insideitem = false;
$tag = &quot;&quot;;
$da_server = &quot;&quot;;
$da_population = &quot;&quot;;
$da_type =&quot;&quot;;
$da_status = &quot;&quot;;
$da_totalpop=0;

function startElement($parser, $tagName, $attrs) {
global $insideitem,$tag, $da_server, $da_type;
if ($insideitem) {
$tag = $tagName;
} elseif ($tagName == &quot;SERVER&quot;)
{

$insideitem = true;
while (list ($key, $val) = each ($attrs)) {
switch($key) {
case &quot;NAME&quot;: $da_server=$val;break;
case &quot;TYPE&quot;: $da_type=$val;break;
} // end case
} // end while
}

}

function characterData($parser, $data) {
global $insideitem, $tag, $da_server, $da_population, $da_type, $da_status,$da_totalpop;
if ($insideitem) {

switch ($tag) {
case &quot;POPULATION&quot;: $da_population .= $data; $da_totalpop += $data; break;
case &quot;STATUS&quot;: $da_status .= $data; break;
}
}
}

function endElement($parser, $tagName) {
global $insideitem, $tag, $da_server, $da_population, $da_type, $da_status;
if ($tagName == &quot;SERVER&quot;) {
print &quot;<TR><TD><b>$da_server</b>&quot;;
if ($da_type) { print &quot;(<i>$da_type</i>)&quot;; }
print &quot;</TD>&quot;;
print &quot;<TD>$da_status</TD>&quot;;
print &quot;<TD>$da_population</TD></TR>\n&quot;;

$da_server = &quot;&quot;;
$da_population = &quot;&quot;;
$da_status = &quot;&quot;;
$da_type = &quot;&quot;;
$insideitem = false;
}
}

// gotta have this for the php-NUKE block stuff to work
global $da_totalpop;

// Create an XML parser
$xml_parser = xml_parser_create();

// Set the functions to handle opening and closing tags
xml_set_element_handler($xml_parser, &quot;startElement&quot;, &quot;endElement&quot;);

// Set the function to handle blocks of character data
xml_set_character_data_handler($xml_parser, &quot;characterData&quot;);

// print &quot;<hr>\n&quot;;
print &quot;<TABLE BORDER=0 WIDTH=0%>\n<TR>\n&quot;;
print &quot;<TD><B>Server</B></TD><TD><B>Status</B></TD><TD><B>Users</B></TD>\n&quot;;
print &quot;</TR>\n&quot;;

// Open the XML file for reading
$fp = fopen(&quot; or die(&quot;Error reading RSS data.&quot;);

// 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(&quot;XML error: %s at line %d&quot;,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));

// Close the XML filef
fclose($fp);

print &quot;<TR><TD>Total Users</TD><TD></TD><TD>$da_totalpop</TD></TR>\n&quot;;
print &quot;</TABLE>\n&quot;;

// free up used memory
xml_parser_free($xml_parser);

?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top