| 194 | }; |
| 195 | |
| 196 | void Document::from_xml(const std::string& xml) |
| 197 | { |
| 198 | m_depth = 0; |
| 199 | if (root) |
| 200 | { |
| 201 | delete root; |
| 202 | } |
| 203 | root = nullptr; |
| 204 | |
| 205 | XML_Parser parser = XML_ParserCreate("UTF-8"); |
| 206 | |
| 207 | XML_SetUserData(parser, this); |
| 208 | |
| 209 | XML_SetDoctypeDeclHandler( |
| 210 | parser, |
| 211 | Document::Expat::start_doctype_decl_handler, |
| 212 | Document::Expat::end_doctype_decl_handler |
| 213 | ); |
| 214 | |
| 215 | XML_SetElementHandler( |
| 216 | parser, |
| 217 | Document::Expat::start_element_handler, |
| 218 | Document::Expat::end_element_handler |
| 219 | ); |
| 220 | |
| 221 | XML_SetCharacterDataHandler( |
| 222 | parser, |
| 223 | Document::Expat::character_data_handler |
| 224 | ); |
| 225 | |
| 226 | XML_Status status = XML_Parse(parser, xml.c_str(), xml.length(), true); |
| 227 | |
| 228 | if (status == XML_STATUS_ERROR) |
| 229 | { |
| 230 | const char* error = XML_ErrorString(XML_GetErrorCode(parser)); |
| 231 | int line = XML_GetCurrentLineNumber(parser); |
| 232 | int column = XML_GetCurrentColumnNumber(parser); |
| 233 | |
| 234 | XML_ParserFree(parser); |
| 235 | |
| 236 | throw Error(error, line, column); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | XML_ParserFree(parser); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | std::string Document::to_xml() const |
| 245 | { |