This is an old question, but I was looking for the answer to this question and could not find it. Now I solved it and thought I should share the code.
- edit , the license, if necessary, for the code below, is used in conjunction with MIT and BSD or something else ...
XPathExtracter.h
#ifndef JOPPLI_XPATHEXTRACTER_H #define JOPPLI_XPATHEXTRACTER_H #include <string> #include <vector> #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/dom/DOM.hpp> namespace Joppli { using namespace xercesc; class XPathExtracter { public: typedef std::vector<std::string> Result; XPathExtracter(); ~XPathExtracter(); DOMDocument * getDocument(const std::string & xml); void extract(const std::string & query, DOMDocument * document, Result * result); protected: DOMLSParser * parser; DOMImplementation * xqillaImplementation; private: static int count; }; } #endif
XPathExtracter.cpp
#include "XPathExtracter.h"
main.cpp
#include <iostream> #include "XPathExtracter.h" int main(void) { std::string * body = new std::string; // ... (logic to fill the body string with an xml/html value) // Extract using namespace xercesc; Joppli::XPathExtracter * driver = new Joppli::XPathExtracter(); Joppli::XPathExtracter::Result * results = new Joppli::XPathExtracter::Result; DOMDocument * document = driver->getDocument(*body); driver->extract("html/head//title", document, results); driver->extract("html/head//meta//@name", document, results); driver->extract("html//body//a[@id=\"link_mx_es\"]", document, results); for(const auto & result : *results) std::cout << result << std::endl; delete results; delete driver; delete body; return 0; }
I ran this code through valgrind and did not detect any leaks.
source share