#[cocos2d-x] HttpRequest , XML
Programming/Old 2016. 3. 6. 18:211. 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
'Programming > Old' 카테고리의 다른 글
#[coco2d-x] How to add event at PageView (0) | 2016.03.07 |
---|---|
#[c++] ssize_t, size_t (0) | 2016.03.07 |
#How to attatch CrossWalk WebView in Cocos2d-x (C++) with Android Studio. (0) | 2016.03.05 |
#Cocos2d VS2013 최적화 (0) | 2016.03.05 |
the difference between desing and architecture. (0) | 2016.03.04 |