解析xml文件有两种方式:Dom和SAX
按照网上的说法,SAX的解析速度比较快,原理这里不细说。直接上实现代码
头文件
#pragma once
/*
* 解析xml工具
*** eg:
* XMLParser *pXmlParser = XMLParser::parseWithFile("xxxx.xml");
* String *pTitle = pXmlParser->getString("xxxx");
*/
#ifndef XML_PARSE_H
#define XML_PARSE_H
#include "cocos2d.h"
#include <string>
#include "platform/CCSAXParser.h"
USING_NS_CC;
class XMLParser : public Object, public SAXDelegator
{
public:
static XMLParser* parseWithFile(const char* xmlFileName);
static XMLParser* parseWithString(const char* content);
// 从本地xml文件读取
bool initWithFile(const char *xmlFileName);
// 从字符中读取,可用于读取网络中的xml数据
bool initWithString(const char *content);
/**
*对应xml标签开始,如:<string name="alex">, name为string,atts为string的属性,如["name","alex"]
*/
virtual void startElement(void *ctx, const char *name, const char **atts);
/**
*对应xml标签结束,如:</string>
*/
virtual void endElement(void *ctx, const char *name);
/**
*对应xml标签文本,如:<string name="alex">Alex Zhou</string>的Alex Zhou
*/
virtual void textHandler(void *ctx, const char *s, int len);
String* getString(const char *key);
public:
XMLParser();
~XMLParser();
private:
Dictionary *m_pDictionary;
std::string m_key;
std::string startXMLElement;
std::string endXMLElement;
};
#endif /* XML_PARSE_H */
源文件:
#include "XMLParser.h"
using namespace std;
// 空格
const static int SPACE = 32;
// 换行
const static int NEXTLINE = 10;
// tab 横向制表符
const static int TAB = 9;
XMLParser* XMLParser::parseWithFile(const char *xmlFileName)
{
XMLParser *pXMLParser = new XMLParser();
if (pXMLParser->initWithFile(xmlFileName))
{
pXMLParser->autorelease();
return pXMLParser;
}
CC_SAFE_DELETE(pXMLParser);
return NULL;
}
bool XMLParser::initWithFile(const char *xmlFileName)
{
m_pDictionary = new Dictionary();
SAXParser _parser;
_parser.setDelegator(this);
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(xmlFileName);
return _parser.parse(fullPath);
}
XMLParser* XMLParser::parseWithString(const char *content)
{
XMLParser *pXMLParser = new XMLParser();
if (pXMLParser->initWithString(content))
{
pXMLParser->autorelease();
return pXMLParser;
}
CC_SAFE_DELETE(pXMLParser);
return NULL;
}
bool XMLParser::initWithString(const char *content)
{
m_pDictionary = new Dictionary();
SAXParser _parse;
if (false == _parse.init("UTF-8"))
return false;
_parse.setDelegator(this);
return _parse.parse(content, strlen(content));
}
void XMLParser::startElement(void *ctx, const char *name, const char **atts)
{
this->startXMLElement = (char *)name;
log("start=%s", startXMLElement.c_str());
if (this->startXMLElement == "string")
{
while (atts && *atts)
{
const char *attsKey = *atts;
if (0 == strcmp(attsKey, "name"))
{
++atts;
const char *attsValue = *atts;
m_key = attsValue;
break;
}
++atts;
}
}
}
void XMLParser::endElement(void *ctx, const char *name)
{
this->endXMLElement = (char *)name;
log("end=%s", endXMLElement.c_str());
}
void XMLParser::textHandler(void *ctx, const char *s, int len)
{
string value((char *)s, 0, len);
//是否全是非正常字符
bool noValue = true;
for (int i = 0; i < len; ++i)
{
if (s[i] != SPACE && s[i] != NEXTLINE && s[i] != TAB)
{
noValue = false;
break;
}
}
if (noValue) return;
String *pString = String::create(value);
log("key=%s value=%s", m_key.c_str(), pString->getCString());
this->m_pDictionary->setObject(pString, this->m_key);
}
String* XMLParser::getString(const char *key)
{
string strKey(key);
return (String *)this->m_pDictionary->objectForKey(strKey);
}
XMLParser::XMLParser()
{
}
XMLParser::~XMLParser()
{
CC_SAFE_DELETE(this->m_pDictionary);
}