例 – XML_Statistics の使用例

使用例

以下の例は、XML_Statistics で文書を分析する方法を示したものです。

Basic example

<?php
require_once "XML/Statistics.php";
$stat = new XML_Statistics(array("ignoreWhitespace" => true));
$result $stat->analyzeFile("example.xml");

if (
$stat->isError($result)) {
    die(
"Error: " $result->getMessage());
}

// タグの合計:
echo "Total tags: " $stat->countTag() . "<br />";

// すべてのタグの中で'title'属性をもつタグの合計
echo "Occurences of attribute title: " $stat->countAttribute("title") . "<br />";

// <section>タグだけの中で'title' 属性をカウントする
echo "Occurences of attribute title in tag section: " $stat->countAttribute("title""section") . "<br />";

// 4番目の階層のタグをカウントする
echo "Amount of Tags in depth 4: " $stat->countTagsInDepth(4) . "<br />";

echo 
"Occurences of PHP Blocks: " $stat->countPI("PHP") . "<br />";

echo 
"Occurences of external entity 'bar': " $stat->countExternalEntity("bar") . "<br />";

echo 
"Data chunks: " $stat->countDataChunks() . "<br />";

echo 
"Length of all data chunks: " $stat->getCDataLength() . "<br />";
?>