///////////////////////////////////////////////////////////////// // xmlParser.cpp - prototype demonstration program // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2002 // ///////////////////////////////////////////////////////////////// /* Note: - This demonstration uses a tokenizer to look for XML elements. You may find it just as easy, and simpler to directly scan the XML string, read from file. - This prototype has no error handling. */ #include #include #include "tok.h" using namespace std; /////////////////////////////////////////////////////////////// // xmlParse class declaration class xmlParse { public: xmlParse(istream &in) : toker(in), level(0) { }; bool getElem(const std::string &parent); bool nextElem(); void indent(); private: token toker; int level; }; //----< send spaces to output to indent display line >--------- void xmlParse::indent() { for(int i=0; i------ bool xmlParse::getElem(const std::string &parent) { const int delta = 3; // get element name and display it std::string tok, name; if(toker.getTok(tok)) { name = tok; cout << "\n "; indent(); cout << "element " << name; } if(parent.size() > 0) cout << ", child of " << parent; // ignoring attributes look for child elements or end while(toker.getTok(tok)) { if(tok == string("<")) { level += delta; getElem(name); } else if(tok == string("' level -= delta; return toker.good(); } } return false; } //----< find start of next element >--------------------------- // // skips over opening banner // bool xmlParse::nextElem() { std::string tok; while(toker.getTok(tok)) if(tok == string("<")) return true; return false; } // //----< test stub >-------------------------------------------- void main(int argc, char *argv[]) { if(argc==1) { cout << "\n Please enter name of XML file to parse\n\n"; return; } ifstream in; for(int i=1; i