#include#include using namespace cppxmlparse; int main() { DOMParser parser; XMLDocument doc = parser.parseFile("example.xml"); // access elements and attributes XMLElement* root = doc.getRootElement(); std::string value = root->getAttribute("attribute"); std::cout << "Attribute value: " << value << std::endl; // iterate through child elements for(XMLElement* child = root->getFirstChildElement(); child != nullptr; child = child->getNextSiblingElement()) { std::string name = child->getName(); std::string text = child->getText(); std::cout << "Element name: " << name << ", text: " << text << std::endl; } return 0; }
#includePackage/Library: Cpp Xml Parse is a library that is primarily distributed as a header-only library for C++.#include using namespace cppxmlparse; class MyHandler : public SAXHandler { public: virtual void onElementStart(const std::string& name, const AttributeList& attrs) { std::cout << "Element start: " << name << std::endl; for(auto& attr : attrs) { std::cout << "Attribute: " << attr.first << "=" << attr.second << std::endl; } } virtual void onElementEnd(const std::string& name) { std::cout << "Element end: " << name << std::endl; } virtual void onText(const std::string& text) { std::cout << "Text: " << text << std::endl; } }; int main() { SAXParser parser; MyHandler handler; parser.setHandler(&handler); parser.parseFile("example.xml"); return 0; }