PHP - XML - Counting XML Element Frequency

Count the frequency of occurrence of a particular element or attribute in an XML document.

Use PEAR's XML_Statistics class:

 

  1. <?php
  2. $xmlData =<<< END
  3. <?xml version='1.0'?>
  4. <library>
  5.     <movie>
  6.         <title>The Matrix</title>
  7.         <cast>
  8.                 <person>Keanu Reeves</person>
  9.                 <person>Laurence Fishburne</person>
  10.                 <person>Carrie-Anne Moss</person>
  11.         </cast>
  12.    </movie>
  13.    <movie>
  14.        <title rating="3">Mission: Impossible III</title>
  15.        <cast>
  16.                <person>Tom Cruise</person>
  17.                <person>Ving Rhames</person>
  18.                <person>Laurence Fishburne</person>
  19.            </cast>
  20.        </movie>
  21.        <movie>
  22.            <title rating="5">Minority Report</title>
  23.            <cast>
  24.                <person>Tom Cruise</person>
  25.                <person>Max von Sydow</person>
  26.            </cast>
  27.        </movie>
  28.    </library>
  29. END;
  30. // include class
  31. include "XML/Statistics.php";
  32. // analyze XML string
  33. $obj = new XML_Statistics();
  34. $obj->analyzeString($xmlData);
  35. // count total number of elements
  36. // result: "Total number of elements: 18"
  37. echo "Total number of elements: " . $obj->countTag() . "/n";
  38. // count total number of <person> elements
  39. // result: "Total number of  elements: 8"
  40. echo "Total number of <person> elements: " .↩
  41. $obj->countTag('person') . "/n";
  42. // count total number of attributes
  43. // result: "Total number of attributes: 2"
  44. echo "Total number of attributes: " . $obj->countAttribute() . "/n";
  45. // count total number of text elements
  46. // result: "Total number of text elements: 11"
  47. echo "Total number of text elements: " . $obj->countDataChunks() . "/n";
  48. ?>

Comments

PEAR's XML_Statistics class, available from http://pear.php.net/package/XML_Statistics, enables you to retrieve information on the number of elements, attributes, CDATA blocks, PIs, and entities within an XML file or string. Once an object of the class is initialized, the analyzeString() or analyzeFile() method statistically analyzes the XML data and builds an internal frequency table for the data within it. The countTag(), countAttribute(), and countDataChunks() methods can then be used to obtain totals for the number of elements, attributes, and character data blocks respectively; these totals may be further filtered by supplying a specific element or attribute name to the corresponding method.

Note 

This example also requires the XML_Parser class from http://pear.php.net/package/XML_Parser.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值