1
/* This is simple demonstration of how to use expat. This program
2
reads an XML document from standard input and writes a line with the
3
name of each element to standard output indenting child elements by
4
one tab stop more than their parent element.
6
Copied from elements.c 98/08/31 AD to open specified file rather than stdin
7
and added a CharacterDataHandler
14
class myParser : public expatpp {
16
myParser() : mDepth(0) {};
18
virtual void startElement(const XML_Char *name, const XML_Char **atts);
19
virtual void endElement(const XML_Char* name);
20
virtual void charData(const XML_Char *s, int len);
29
myParser::WriteIndent()
31
for (int i = 0; i < mDepth; i++)
37
myParser::startElement(const char* name, const char** atts)
41
if (atts) { /* write list of attributes indented below element */
43
for (i=0; atts[i]; i++) {
45
putchar('-'); putchar(' ');
54
myParser::endElement(const char*)
61
myParser::charData(const XML_Char *s, int len)
63
const int leadingSpace = skipWhiteSpace(s);
64
if (len==0 || len==leadingSpace)
65
return; // called with whitespace between elements
69
/* write out the user data bracketed by ()*/
71
fwrite(s, len, 1, stdout);
86
puts("\n\nXML test: enter filename");
87
fgets(filename,80,stdin);
88
if (strlen(filename)==0)
91
xmlFile = fopen(filename, "r");
96
size_t len = fread(buf, 1, sizeof(buf), xmlFile /*stdin*/);
97
done = len < sizeof(buf);
98
if (!parser.XML_Parse(buf, len, done)) {
101
XML_ErrorString(parser.XML_GetErrorCode()),
102
parser.XML_GetCurrentLineNumber());
109
puts("\nfinished! - press CR to exit");
110
fgets(filename,80,stdin);