|
1 |
| -# rapidxml |
2 |
| -CMake compatible and restructured mirror of http://rapidxml.sourceforge.net/ |
| 1 | +# Overview |
| 2 | + |
| 3 | +RapidXml is an attempt to create the fastest XML parser possible, while retaining useability, portability and reasonable W3C compatibility. It is an in-situ parser written in modern C++, with parsing speed approaching that of strlen function executed on the same data. |
| 4 | + |
| 5 | +RapidXml has been around since 2006, and is being used by lots of people. HTC uses it in some of its mobile phones. |
| 6 | + |
| 7 | +If you are looking for a stable and fast parser, look no further. Integration with your project will be trivial, because entire library is contained in a single header file, and requires no building or configuration. |
| 8 | + |
| 9 | +This is a CMake compatible and restructured mirror of http://rapidxml.sourceforge.net/. |
| 10 | + |
| 11 | +# Integration |
| 12 | +It is recommended to use [CPM](https://github.com/cpm-cmake/CPM.cmake) for integrating this libray. After adding [CPM.cmake](https://github.com/cpm-cmake/CPM.cmake/blob/master/cmake/CPM.cmake) to your `cmake/` directory, simply add the following to your top-level `CMakeLists.txt` to include the library in your project: |
| 13 | +```cmake |
| 14 | +include(cmake/CPM.cmake) |
| 15 | +CPMAddPackage("gh:CodeFinder2/rapidxml@1.13") |
| 16 | +
|
| 17 | +add_executable(my_app ...) |
| 18 | +target_link_libraries(my_app rapidxml) |
| 19 | +``` |
| 20 | + |
| 21 | +# Usage |
| 22 | +Here's a small example on how to use it to parse a [GraphML](https://en.wikipedia.org/wiki/GraphML): |
| 23 | +```c++ |
| 24 | +#include <iostream> |
| 25 | + |
| 26 | +#include <rapidxml/rapidxml.hpp> |
| 27 | +#include <rapidxml/rapidxml_utils.hpp> |
| 28 | + |
| 29 | +using namespace std; |
| 30 | + |
| 31 | +int main(int argc, char *argv[]) |
| 32 | +{ |
| 33 | + try { |
| 34 | + namespace rx = rapidxml; |
| 35 | + // Read graph properties manually: |
| 36 | + rx::file<> file(argv[1]); // (default template is char) |
| 37 | + rx::xml_document<> doc; |
| 38 | + doc.parse<0>(file.data()); |
| 39 | + |
| 40 | + rx::xml_node<> *n_graphml = doc.first_node("graphml"); // root tag of the document |
| 41 | + if (n_graphml) { |
| 42 | + for (rx::xml_node<> *n = n_graphml->first_node("key"); n; n = n->next_sibling("key")) { |
| 43 | + rx::xml_attribute<> *intent = n->first_attribute("for"); |
| 44 | + if (!intent || !intent->value()) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + // Get an attribute from <key> tag: |
| 48 | + rx::xml_attribute<> *id = n->first_attribute("id"); |
| 49 | + |
| 50 | + // If everything is valid, try to extract the data: |
| 51 | + string data; |
| 52 | + getData(n->first_node("default"), data, false); |
| 53 | + // ... |
| 54 | + } |
| 55 | + } |
| 56 | + } catch (exception &e) { |
| 57 | + cerr << e.what() << endl; |
| 58 | + return EXIT_FAILURE; |
| 59 | + } |
| 60 | + return EXIT_SUCCESS; |
| 61 | +} |
| 62 | +``` |
0 commit comments