请教一个关于 C++ Poco XML SAX 解析 相关的问题

期货大佬 发布于 2018/11/30 12:02
阅读 228
收藏 0

开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>

#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";
}

 

 XMLHandler.h

#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

 

XMLHandler.cpp

#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));
}

 

解析好了 可以控制台输出 但是不知道 怎么拿出来用

加载中
0
qwsdcv
qwsdcv

建议你换成DOM处理 而不是用SAX。如果可以的话最好换成Java, 使用JAXB避免解析XML的繁琐。事实上手工解析XML是程序员的心智负担。

#include <string>
#include <cstdio>

#include <Poco/XML/XML.h>
#include <Poco/XML/XMLString.h>
#include <Poco/SAX/Attributes.h>

#include <Poco/SAX/SAXParser.h>
#include <Poco/SAX/DefaultHandler.h>

using namespace std;
using namespace Poco::XML;

struct Bean
{
    string name;
    string recommend;
};

class MyHandle : public DefaultHandler
{
  public:
    virtual void startElement(const XMLString &uri, const XMLString &localName, const XMLString &qname, const Attributes &attrList)
    {
        current_tag = localName;
        if (localName == "element")
        {
            bean.name = attrList.getValue("", "name");
        }
    }

    virtual void characters(const XMLChar ch[], int start, int length)
    {
        if (current_tag == "recommend")
        {
            bean.recommend = string(ch).substr(start, length);
        }
    }

    virtual void endElement(const XMLString &uri, const XMLString &localName, const XMLString &qname)
    {
        current_tag = "";
    }

    Bean get()
    {
        return bean;
    }

  protected:
    Bean bean;

  private:
    string current_tag;
};

int main()
{

    string content = "<root>\
    <element name=\"sax\"/> \
    <recommend>false</recommend> \
    </root>";

    SAXParser sax;
    MyHandle handler;
    sax.setContentHandler(&handler);
    sax.parseString(content);
    Bean b = handler.get();
    printf("Bean.name:%s, Bean.recommend:%s\n", b.name.c_str(), b.recommend.c_str());
}

 

期货大佬
期货大佬
非常感谢
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部
返回顶部
顶部