遊戲後期,但可能仍然有用。我正在研究Poco XML以提取以xml傳遞的天氣數據。我發現PDF-slide @Jently是一個很好的介紹。這提供了hpp文件。這個example涵蓋了實現。我省略了LexicalHandler。
我查看字符串listOfReactions,發現時,我將屬性名和值添加到startElement()中的字符串中。在字符()中,我將節點中的文本添加到字符串中,並將其添加到可以遍歷的矢量中。
輸出:
ID = J1,可逆=假,假動作
ID = J2,可逆=真,真招
我改變你的XML稍微進行測試,並且逃脫用於程序中的雙引號。
<?xml version=\"1.0\" encoding=\"UTF-8\"?><sbml xmlns = \"http://www.sbml.org/sbml/level2/version4\" level = \"2\" version = \"4\">
<model id = \"cell\">
<listOfSpecies>species</listOfSpecies>
<listOfParameters>
<parameter id = \"kk1\" value = \"1\"/>
</listOfParameters>
<listOfReactions>
<reaction id = \"J1\" reversible = \"false\">false move</reaction>
<reaction id = \"J2\" reversible = \"true\">true move</reaction>
</listOfReactions>
</model>
</sbml>
主:
#include <iostream>
#include "MyHandler.hpp"
using namespace std;
int main() {
auto s = {XML file from above};
MyHandler handler {};
Poco::XML::SAXParser parser {};
parser.setFeature(Poco::XML::XMLReader::FEATURE_NAMESPACES, false);
parser.setFeature(Poco::XML::XMLReader::FEATURE_NAMESPACE_PREFIXES, true);
parser.setContentHandler(&handler);
try {
parser.parseString(s);
} catch (Poco::Exception& e) {
cerr << e.displayText() << endl;
}
auto saved = handler.saved_reactions();
for (auto& i : saved) {
cout << i << endl;
}
return 0;
}
MyHandler.hpp:
#ifndef MYHANDLER_HPP_
#define MYHANDLER_HPP_
#include <iostream>
#include <vector>
#include <Poco/SAX/Attributes.h>
#include <Poco/SAX/ContentHandler.h>
#include <Poco/SAX/SAXParser.h>
class MyHandler: public Poco::XML::ContentHandler {
public:
MyHandler();
virtual ~MyHandler();
// ContentHandler overrides, begin.
void setDocumentLocator(const Poco::XML::Locator* loc);
void startDocument();
void endDocument();
void startElement(
const Poco::XML::XMLString&,
const Poco::XML::XMLString&,
const Poco::XML::XMLString&,
const Poco::XML::Attributes&);
void endElement(
const Poco::XML::XMLString&,
const Poco::XML::XMLString&,
const Poco::XML::XMLString&);
void characters(const Poco::XML::XMLChar ch[], int, int);
void ignorableWhitespace(const Poco::XML::XMLChar ch[], int, int);
void processingInstruction(const Poco::XML::XMLString&, const Poco::XML::XMLString&);
void startPrefixMapping(const Poco::XML::XMLString&, const Poco::XML::XMLString&);
void endPrefixMapping(const Poco::XML::XMLString&);
void skippedEntity(const Poco::XML::XMLString&);
// ContentHandler overrides, end
std::vector<std::string> saved_reactions();
private:
bool show = false;
std::string reactions_s {};
std::vector<std::string> reactions_v {};
};
#endif /* MYHANDLER_HPP_ */
MyHandler.cpp:
#include "MyHandler.hpp"
MyHandler::MyHandler() {}
MyHandler::~MyHandler() {}
void MyHandler::setDocumentLocator(const Poco::XML::Locator* loc) {
}
void MyHandler::startDocument() {
}
void MyHandler::endDocument() {
}
void MyHandler::startElement(const Poco::XML::XMLString& namespaceURI, const Poco::XML::XMLString& localName, const Poco::XML::XMLString& qname, const Poco::XML::Attributes& attributes) {
int x {0};
std::cout << "qname: " << qname << std::endl;
/* std::cout << "getValue(): " << attributes.getValue(qname) << std::endl;
std::cout << "getLength(): " << attributes.getLength() << std::endl;*/
if (qname == "listOfReactions") {
show = true;
}
if (show) {
if (attributes.getLength()) {
reactions_s.clear();
x = attributes.getLength();
for (int i = 0; i < x; ++i) {
std::cout << "getQName(): " << attributes.getQName(i) << ", getValue(): " << attributes.getValue(i) << std::endl;
if (reactions_s.size()) reactions_s += ",";
reactions_s += attributes.getQName(i) + "=" + attributes.getValue(i);
}
}
}
}
void MyHandler::endElement(const Poco::XML::XMLString& allocator,
const Poco::XML::XMLString& allocator1,
const Poco::XML::XMLString& allocator2) {
}
void MyHandler::characters(const Poco::XML::XMLChar ch[], int start, int length) {
std::cout << std::string(ch + start, length) << std::endl;
if (show) {
reactions_s += "," + std::string(ch + start, length);
reactions_v.emplace_back(reactions_s);
}
}
void MyHandler::ignorableWhitespace(const Poco::XML::XMLChar ch[], int start, int length) {
}
void MyHandler::processingInstruction(const Poco::XML::XMLString& allocator, const Poco::XML::XMLString& allocator1) {
}
void MyHandler::startPrefixMapping(const Poco::XML::XMLString& allocator, const Poco::XML::XMLString& allocator1) {
}
void MyHandler::endPrefixMapping(const Poco::XML::XMLString& allocator) {
}
std::vector<std::string> MyHandler::saved_reactions() {
return reactions_v;
}
void MyHandler::skippedEntity(const Poco::XML::XMLString& allocator) {
}
那麼,你嘗試過什麼? – Alon 2013-03-25 22:30:21
我試過他們的例子,涉及到一個DOM解析器。我能夠到達節點,但不知道是否會提取其內容。我可以打印pNode-> getNodeValue(),但不會打印任何內容。 – 2013-03-25 22:34:00
節點「listOfReactions」不包含該值。它有一個名爲「#text」的子節點,其中包含值。這並不明顯,但是在執行「pNode-> firstNode() - > nodeValue()」時,您將獲得該值。 – 2017-01-20 15:09:47