1 using System;
2 using System.Xml;
3
4 class XmlStats : XmlContentHandler
5 {
6 public XmlStats(const string& xmlFileName_) : xmlFileName(xmlFileName_)
7 {
8 }
9 public override void StartElement(const ustring& namespaceUri, const ustring& localName, const ustring& qualifiedName, const Attributes& attributes)
10 {
11 ++numElements;
12 numAttributes = numAttributes + attributes.Count();
13 }
14 public override void HandlePI(const ustring& target, const ustring& data)
15 {
16 ++numProcessingInstructions;
17 }
18 public override void HandleComment(const ustring& comment)
19 {
20 ++numComments;
21 }
22 public override void HandleCDataSection(const ustring& cdata)
23 {
24 numCharacters = numCharacters + cdata.Length();
25 }
26 public override void HandleText(const ustring& text)
27 {
28 numCharacters = numCharacters + text.Length();
29 }
30 public void Print()
31 {
32 Console.Out() << xmlFileName << " contains:" << endl();
33 Console.Out() << numElements << " elements" << endl();
34 Console.Out() << numAttributes << " attributes" << endl();
35 Console.Out() << numProcessingInstructions << " processing instructions" << endl();
36 Console.Out() << numComments << " comments" << endl();
37 Console.Out() << numCharacters << " characters" << endl();
38 }
39 private string xmlFileName;
40 private long numElements;
41 private long numAttributes;
42 private long numProcessingInstructions;
43 private long numComments;
44 private long numCharacters;
45 }
46
47 int main(int argc, const char** argv)
48 {
49 try
50 {
51 for (int i = 1; i < argc; ++i;)
52 {
53 string xmlFileName = argv[i];
54 XmlStats stats(xmlFileName);
55 ParseXmlFile(xmlFileName, &stats);
56 stats.Print();
57 }
58 }
59 catch (const Exception& ex)
60 {
61 Console.Error() << ex.Message() << endl();
62 return 1;
63 }
64 return 0;
65 }