#[cocos2d-x] HttpRequest , XML

Programming/Old 2016. 3. 6. 18:21

1. Header File

#pragma once

#include "tinyxml2\tinyxml2.h"
#include "network/HttpClient.h"
#include "cocos2d.h"

using namespace tinyxml2;
using namespace cocos2d::network;
using namespace std;
using namespace cocos2d;


2. Cpp File

#include "XMLTest.h"

void XMLTest::getXmlValue(const char* url)
{
HttpRequest *request = new HttpRequest();
request->setUrl(url);
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(CC_CALLBACK_2(XMLTest::loadXml, this));
HttpClient::getInstance()->send(request);
request->release();
}

void XMLTest::loadXml(HttpClient *sender, HttpResponse *response)
{
if (!response->isSucceed())
{
return;
}

vector<char> *buffer = response->getResponseData();
string _xml = string(buffer->begin(), buffer->end());

tinyxml2::XMLDocument doc;
if (doc.Parse(_xml.c_str()) == XML_NO_ERROR)
{
const XMLElement *test = doc.FirstChildElement();
for (; test; test = test->NextSiblingElement())
{
const XMLElement *_x = test->FirstChildElement("question");
__String *xStr = __String::create(_x->GetText());

const XMLElement *_y = test->FirstChildElement("answer");
__String *yStr = __String::create(_y->GetText());

CCLOG("x:%s y:%s", xStr->getCString(), yStr->getCString());
}
}
}


3. 사용

XMLTest *model = new XMLTest();
XMLTest->getXmlValue("http://zetawiki.com/ex/php/quiz.xml");


4. 출처 : http://albatrus.com/main/cocos2d/6239

admin