00001 #include <string> 00002 #include <iostream> 00003 #include <fstream> 00004 #include "easyxml.hxx" 00005 00006 using std::string; 00007 using std::ifstream; 00008 using std::cout; 00009 using std::cerr; 00010 using std::endl; 00011 00012 class MyVisitor : public XMLVisitor 00013 { 00014 public: 00015 virtual void startXML () { 00016 cout << "Start XML" << endl; 00017 } 00018 virtual void endXML () { 00019 cout << "End XML" << endl; 00020 } 00021 virtual void startElement (const char * name, const XMLAttributes &atts) { 00022 cout << "Start element " << name << endl; 00023 for (int i = 0; i < atts.size(); i++) 00024 cout << " " << atts.getName(i) << '=' << atts.getValue(i) << endl; 00025 } 00026 virtual void endElement (const char * name) { 00027 cout << "End element " << name << endl; 00028 } 00029 virtual void data (const char * s, int len) { 00030 cout << "Character data " << string(s,len) << endl; 00031 } 00032 virtual void pi (const char * target, const char * data) { 00033 cout << "Processing instruction " << target << ' ' << data << endl; 00034 } 00035 virtual void warning (const char * message, int line, int column) { 00036 cout << "Warning: " << message << " (" << line << ',' << column << ')' 00037 << endl; 00038 } 00039 virtual void error (const char * message, int line, int column) { 00040 cout << "Error: " << message << " (" << line << ',' << column << ')' 00041 << endl; 00042 } 00043 }; 00044 00045 int main (int ac, const char ** av) 00046 { 00047 MyVisitor visitor; 00048 00049 for (int i = 1; i < ac; i++) { 00050 ifstream input(av[i]); 00051 cout << "Reading " << av[i] << endl; 00052 try { 00053 readXML(input, visitor); 00054 00055 } catch (const sg_exception& e) { 00056 cerr << "Error: file '" << av[i] << "' " << e.getFormattedMessage() << endl; 00057 return -1; 00058 00059 } catch (...) { 00060 cerr << "Error reading from " << av[i] << endl; 00061 return -1; 00062 } 00063 } 00064 return 0; 00065 }