1 /** 2 * Inspired by: http://dbus-cplusplus.sourceforge.net/ 3 */ 4 5 #ifndef __SDBUSCPP_TOOLS_XML_H 6 #define __SDBUSCPP_TOOLS_XML_H 7 8 9 #include <exception> 10 #include <string> 11 #include <vector> 12 #include <map> 13 #include <memory> 14 #include <iostream> 15 #include <sstream> 16 17 namespace sdbuscpp 18 { 19 namespace xml 20 { 21 22 class Error : public std::exception 23 { 24 public: 25 26 Error(const char *error, int line, int column); 27 ~Error()28 ~Error() {} 29 what()30 const char *what() const noexcept { return m_error.c_str(); } 31 32 private: 33 std::string m_error; 34 }; 35 36 37 class Node; 38 39 class Nodes : public std::vector<Node *> 40 { 41 public: 42 Nodes operator[](const std::string &key) const; 43 Nodes select(const std::string &attr, const std::string &value) const; 44 }; 45 46 class Node 47 { 48 public: 49 50 using Attributes = std::map<std::string, std::string>; 51 using Children = std::vector<Node>; 52 53 std::string name; 54 std::string cdata; 55 Children children; 56 Node(std::string n,Attributes a)57 Node(std::string n, Attributes a) : 58 name(n), 59 m_attrs(a) 60 {} 61 62 Node(const char* n, const char** a = nullptr); 63 64 Nodes operator[](const std::string& key); 65 66 std::string get(const std::string& attribute) const; 67 68 void set(const std::string &attribute, std::string value); 69 70 std::string to_xml() const; 71 add(Node child)72 Node& add(Node child) 73 { 74 children.push_back(child); 75 return children.back(); 76 } 77 78 private: 79 80 void _raw_xml(std::string& xml, int& depth) const; 81 82 Attributes m_attrs; 83 }; 84 85 class Document 86 { 87 public: 88 struct Expat; 89 90 Node* root; 91 92 Document(); 93 ~Document(); 94 95 Document(const std::string& xml); 96 97 void from_xml(const std::string& xml); 98 99 std::string to_xml() const; 100 101 private: 102 103 int m_depth; 104 }; 105 106 } // namespace xml 107 108 } // namespace sdbuscpp 109 110 std::istream &operator >> (std::istream &, sdbuscpp::xml::Document &); 111 std::ostream &operator << (std::ostream &, sdbuscpp::xml::Document &); 112 113 #endif//__SDBUSCPP_TOOLS_XML_H 114