dom.hxx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // file : examples/hybrid/dom.hxx
  2. // copyright : not copyrighted - public domain
  3. #ifndef DOM_HXX
  4. #define DOM_HXX
  5. #include <map>
  6. #include <string>
  7. #include <vector>
  8. #include <xml/qname>
  9. #include <xml/forward>
  10. // A simple, DOM-like in-memory representation of raw XML. It only supports
  11. // empty, simple, and complex content (no mixed content) and is not
  12. // particularly efficient, at least not in C++98.
  13. class element;
  14. typedef std::map<xml::qname, std::string> attributes;
  15. typedef std::vector<element> elements;
  16. class element
  17. {
  18. public:
  19. typedef ::attributes attributes_type;
  20. typedef ::elements elements_type;
  21. element (const xml::qname& name): name_ (name) {}
  22. element (const xml::qname& name, const std::string text)
  23. : name_ (name), text_ (text) {}
  24. const xml::qname&
  25. name () const {return name_;}
  26. const attributes_type&
  27. attributes () const {return attributes_;}
  28. attributes_type&
  29. attributes () {return attributes_;}
  30. const std::string&
  31. text () const {return text_;}
  32. void
  33. text (const std::string& text) {text_ = text;}
  34. const elements_type&
  35. elements () const {return elements_;}
  36. elements_type&
  37. elements () {return elements_;}
  38. public:
  39. // Parse an element. If start_end is false, then don't parse the
  40. // start and end of the element.
  41. //
  42. element (xml::parser&, bool start_end = true);
  43. // Serialize an element. If start_end is false, then don't serialize
  44. // the start and end of the element.
  45. //
  46. void
  47. serialize (xml::serializer&, bool start_end = true) const;
  48. private:
  49. xml::qname name_;
  50. attributes_type attributes_;
  51. std::string text_; // Simple content only.
  52. elements_type elements_; // Complex content only.
  53. };
  54. #endif // DOM_HXX