#ifndef XMLPARSER_H #define XMLPARSER_H ///////////////////////////////////////////////////////////////////// // xmlParser.h - parse XML elements and documents // // ver 1.1 // // Language: Visual C++, ver 7.0 // // Platform: Dell Dimension 8300, Windows XP Pro, SP1.0 // // Application: CSE687 - Object Oriented Design, Pr#3 prototype // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// /* * Module Operations: * ================== * This module defines three classes: * - element : builds an XML element * - document : builds XML document, given a root element * - xmlParser : responsible for dissecting an XML string into its * constituent parts * * Public Interface: * ================= * element e(aTag); * e.body("text for the body"); // e is a complete element * document d(e); // d is a complete document * * xmlParser parse(xmlString); * parse.next(); * std::string elem = parse.element(); * std::string tag = parse.tag(); * std::string name, value; * xmlParser::Attributes attrs = parse.attributes(); * while(attrs.next(name,value)) * cout << "\n name: " << name << ", value: " << value; * std::string body = parse.body(); * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Build Process * ============= * Required files: xmlParser.h, xmlParser.cpp * * Build command: cl /EHsc /DTEST_XML xmlParser.cpp * * Maintenance History: * ==================== * ver 1.0 : 28 Mar 04 * - first release * ver 1.1 : 04 Apr 04 * - fixed bug in xmlParser, reported by Digant Mehta, by initializing * pos to zero in promotion constructor and in xmlString(...) function * (same as calling front()). * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Planned Modifications: * ====================== * - xmlParser: * - add function to extract text from a body containing both * text and child elements * - add recursive descent formatter that puts string into * indented form */ // #include #include "..\\collection\\collection.h" class element : public std::string { public: element(const std::string& tag); void body(const std::string& b); void insert(const std::string& s); void attrib(const std::string& name, const std::string& value); private: std::string _tag; }; class document : public std::string { public: document(const element& e); }; class xmlParser : public std::string { public: typedef collection Attributes; xmlParser() {} xmlParser(const std::string& xmlStr) : std::string(xmlStr), pos(0) {} void xmlString(const std::string& xmlStr); size_t next(); void front(); void back(); xmlParser& operator++(); xmlParser operator++(int); xmlParser& operator--(); xmlParser operator--(int); std::string head(); std::string tail(); std::string tag(); Attributes attributes(); std::string body(); std::string element(); std::string text(); // incomplete implementation std::string formatter(bool start=true); private: size_t pos; int level; std::string formed; }; // //----< set new string >--------------------------------------------- inline void xmlParser::xmlString(const std::string& xmlStr) { *this = xmlStr; pos = 0; } //----< point to front of xml string >------------------------------- inline void xmlParser::front() { pos = 0; } //----< point to one past end of xml string >------------------------ inline void xmlParser::back() { pos = size(); } //----< pre-increment >---------------------------------------------- inline xmlParser& xmlParser::operator++() { ++pos; return *this; } //----< pre-decrement >---------------------------------------------- inline xmlParser& xmlParser::operator--() { --pos; return *this; } //----< return from current location to end of xml string >---------- inline std::string xmlParser::tail() { return substr(pos,size()-pos); } //----< return beginning to current location of xml string >--------- inline std::string xmlParser::head() { return substr(0,pos); } #endif