SoFunction
Updated on 2025-04-13

PHP XML Analysis Function Code Page 2/2


example
<?
/*****************************************************************************
* Name: XML parsing example: XML document information statistics
* describe
* This example collects and counts XML document information through PHP's Expat parser (for example: the number of occurrences of each element, parent element and child element.
* XML file as a parameter ./xmlstats_PHP4.php3
* $Requires: Expat Requirements: Expat PHP4.0 compiles to CGI mode
*****************************************************************************/
// The first parameter is an XML file
$file = $argv[1];
// Initialization of variables
$elements = $stack = array();
$total_elements = $total_chars = 0;
// Basic class of elements
class element
{
var $count = 0;
var $chars = 0;
var $parents = array();
var $childs = array();
}
// Functions that parse XML files
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
{
die("Can't find file "$file".");
}
if(!($fp = @fopen($file, "r")))
{
die("Can't open file "$file".");
}
while($data = fread($fp, 4096))
{
if(!xml_parse($parser, $data, feof($fp)))
{
return(false);
}
}
fclose($fp);
return(true);
}
// Output result function (box form)
function print_box($title, $value)
{
printf("n+%'-60s+n", "");
printf("|%20s", "$title:");
printf("%14s", $value);
printf("%26s|n", "");
printf("+%'-60s+n", "");
}
// Output result function (line form)
function print_line($title, $value)
{
printf("%20s", "$title:");
printf("%15sn", $value);
}
// Sort function
function my_sort($a, $b)
{
return(is_object($a) && is_object($b) ? $b->count - $a->count: 0);
}
function start_element($parser, $name, $attrs)
{
global $elements, $stack;
// Is the element already in the global $elements array?
if(!isset($elements[$name]))
{
// No - add an element's class instance
$element = new element;
$elements[$name] = $element;
}
// Add one to the counter of this element
$elements[$name]->count++;
// Is there a parent element?
if(isset($stack[count($stack)-1]))
{
// Yes - Assign the parent element to $last_element
$last_element = $stack[count($stack)-1];
// If the current element parent element array is empty, initialize to 0
if(!isset($elements[$name]->parents[$last_element]))
{
$elements[$name]->parents[$last_element] = 0;
}
// Add one to the parent element counter of this element
$elements[$name]->parents[$last_element]++;
// If the child element array of the current parent element of the element is empty, initialize to 0
if(!isset($elements[$last_element]->childs[$name]))
{
$elements[$last_element]->childs[$name] = 0;
}
// Add one child element counter of the parent element of the element
$elements[$last_element]->childs[$name]++;
}
// Add the current element to the stack
array_push($stack, $name);
}
function stop_element($parser, $name)
{
global $stack;
// Remove the top element from the stack
array_pop($stack);
}
function char_data($parser, $data)
{
global $elements, $stack, $depth;
// Increase the number of characters in the current element
$elements[$stack][count($stack)-1]]->chars += strlen(trim($data));
}
// Generate an instance of the parser
$parser = xml_parser_create();
// Set processing functions
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "char_data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
// parse the file
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
// Release the parser
xml_parser_free($parser);
// Release the assistance element
unset($elements["current_element"]);
unset($elements["last_element"]);
// Sort by number of elements
uasort($elements, "my_sort");
// Loop up element information in $elements
while(list($name, $element) = each($elements))
{
print_box("Element name", $name);
print_line("Element count", $element->count);
print_line("Character count", $element->chars);
printf("n%20sn", "* Parent elements");
// Loop in the parent of this element and output the result
while(list($key, $value) = each($element->parents))
{
print_line($key, $value);
}
if(count($element->parents) == 0)
{
printf("%35sn", "[root element]");
}
// Loop in the child of this element and output the result
printf("n%20sn", "* Child elements");
while(list($key, $value) = each($element->childs))
{
print_line($key, $value);
}
if(count($element->childs) == 0)
{
printf("%35sn", "[no childs]");
}
$total_elements += $element->count;
$total_chars += $element->chars;
}
// Final result
print_box("Total elements", $total_elements);
print_box("Total characters", $total_chars);
?>
Previous page12Read the full text