To analyze XML documents, you can install and use the XML_Statistics package, which provides methods for obtaining statistics about tags, attributes, entities, processing instructions, data blocks, and CDATA sections for any well-formatted XML document.
To install the PEAR, use this command (version 0.1 is the beta version):
-> pear install -alldeps XML_Statistics-0.1
XML document used for test purposes in the example code.
The following PHP example uses the XML document in Listing 1 to retrieve some basic statistics about tags, attributes, and CDATA sections:
PHP Code:
<?php
//import Statistics.php
require_once 'XML/Statistics.php';
//ignore whitespaces
$stat = new XML_Statistics(array("ignoreWhitespace" => true));
//analyze a file or URL
$result = $stat->analyzeFile("myxml.xml");
if ($stat->isError($result)) {
die("Error: " . $result->getMessage());
}
else
{
// total number of tags
echo "Total tags: " . $stat->countTag()."<br>";
// count number of 'type' attribute
echo "Occurences of attribute type: " . $stat->countAttribute("type")."<br>";
// get the maximum depth
echo "Maximum depth: " . $stat->getMaxDepth()."<br>";
// count total number of tags in depth 3
echo "The number of tags in depth 3: " . $stat->countTagsInDepth(3)."<br>";
// count the occurences of data blocks
echo "Data chunks: " . $stat->countDataChunks()."<br>";
// get the length of all CData sections
echo "Length of all data chunks: " . $stat->getCDataLength()."<br>";
}
?>
The output of this example is:
Total tags: 16
Occurences of attribute type: 2
Maximum depth: 3
The number of tags in depth 3: 10
Data chunks: 6
Length of all data chunks: 93
Bookmarks