#include "HTTPRequestHome.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTMLForm.h"
#include "Poco/StreamCopier.h"
#include "Poco/SAX/SAXParser.h"
#include "XMLHandler.h"
using namespace Poco;
using namespace std;
using namespace Poco::XML;
void HTTPRequestHome::handleRequest(HTTPServerRequest &request, HTTPServerResponse &response) {
response.setStatus(HTTPResponse::HTTP_OK);
ostream &stream = response.send();
if (request.getMethod() == HTTPServerRequest::HTTP_GET) {
HTMLForm form(request);
stream << form.get("echostr");
return;
}
string data;
StreamCopier::copyToString(request.stream(), data);
XMLHandler handler;
SAXParser parser;
parser.setContentHandler(&handler);
parser.parseString(data);
// 已经解析完了 可是要怎么拿到这里来用呢?
stream << "success";
}
#ifndef QUNAR_XMLHANDLER_H
#define QUNAR_XMLHANDLER_H
#include "Poco/SAX/ContentHandler.h"
using namespace Poco::XML;
using namespace std;
class XMLHandler : public ContentHandler {
public:
void setDocumentLocator(const Locator *loc) override {};
void startDocument() override {};
void endDocument() override {}
void startElement(const XMLString &uri, const XMLString &localName, const XMLString &qname,
const Attributes &attrList) override;
void endElement(const XMLString &uri, const XMLString &localName, const XMLString &qname) override {};
void characters(const XMLChar ch[], int start, int length) override;
void ignorableWhitespace(const XMLChar ch[], int start, int length) override {};
void processingInstruction(const XMLString &target, const XMLString &data) override {};
void startPrefixMapping(const XMLString &prefix, const XMLString &uri) override {};
void endPrefixMapping(const XMLString &prefix) override {};
void skippedEntity(const XMLString &name) override {};
};
#endif //QUNAR_XMLHANDLER_H
#include <iostream>
#include "XMLHandler.h"
void XMLHandler::startElement(const XMLString &uri, const XMLString &localName, const XMLString &qname,
const Attributes &attrList) {
cout << localName << endl;
}
void XMLHandler::characters(const XMLChar *ch, int start, int length) {
cout << static_cast<string>(ch).substr(static_cast<unsigned long>(start), static_cast<unsigned long>(length));
}
解析好了 可以控制台输出 但是不知道 怎么拿出来用