cocos2d-x解析xml文件
解析xml文件可以是使用第三方的开源库,这里我们谁用TinyXML,这里是这个库的下载地址 http://sourceforge.net/projects/tinyxm
我们用的只有里面的两个头文件tinystr.h、tinyxml.h和4个cpp文件tinystr.cpp、tinycml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp,xml的解析主要是通过获取节点的方法来遍历整个文本,主要用到的是TiXmlNode、TiXmlElement和TiXmlAttribute(获取元素的属性)这三个类,其中TiXmlNode是TiXmlElement的父类,将这几个文件直接放在Classes里面就可以用了。例子中的用法和以前的例子做json解析类似,都是讲文本读到一个tableview上显示,具体看代码。
代码如下:
复制代码
运行效果如下:
解析xml文件可以是使用第三方的开源库,这里我们谁用TinyXML,这里是这个库的下载地址 http://sourceforge.net/projects/tinyxm
我们用的只有里面的两个头文件tinystr.h、tinyxml.h和4个cpp文件tinystr.cpp、tinycml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp,xml的解析主要是通过获取节点的方法来遍历整个文本,主要用到的是TiXmlNode、TiXmlElement和TiXmlAttribute(获取元素的属性)这三个类,其中TiXmlNode是TiXmlElement的父类,将这几个文件直接放在Classes里面就可以用了。例子中的用法和以前的例子做json解析类似,都是讲文本读到一个tableview上显示,具体看代码。
代码如下:
- bool XmlParser::init()
- {
- if(!CCLayer::init())
- {
- return false;
- }
- dictionary=CCDictionary::create();
- dictionary->retain();
- size=CCDirector::sharedDirector()->getWinSize();
- CCSprite* background=CCSprite::create("background.jpg");
- background->setPosition(ccp(size.width*0.5,size.height*0.5));
- this->addChild(background);
-
- parseXml();
-
- CCTableView* viewLayer=CCTableView::create(this, CCSizeMake(430, 600));
- viewLayer->setAnchorPoint(ccp(0.5,0.5));
- viewLayer->setDirection(kCCScrollViewDirectionVertical);
- viewLayer->setVerticalFillOrder(kCCTableViewFillTopDown);
- viewLayer->setPosition(ccp(background->getContentSize().width/2-220,background->getContentSize().height/2-400));
- viewLayer->setContentOffset(ccp(0, 600-viewLayer->getContentSize().height));
- viewLayer->setDelegate(this);
- this->addChild(viewLayer);
-
- return true;
- }
- void XmlParser::parseXml()//解析xml
- {
- int index=0;
- GradeInfo* firstInfo=GradeInfo::getInstance();
-
- std::string xmlFile=CCFileUtils::sharedFileUtils()->fullPathForFilename("perinfo.xml");
- TiXmlDocument* xmlData=new TiXmlDocument(xmlFile.c_str());//初始化一个xml文档对象
- xmlData->LoadFile();
-
- TiXmlElement* rootNode=xmlData->RootElement();//每个xml文件对应一个唯一根节点,这里获取根节点。
- CCLog("%s",rootNode->Value());
-
- TiXmlElement* firstNode=rootNode->FirstChildElement();//获取根节点的第一个子节点
- TiXmlElement* nameTag=firstNode->FirstChildElement();//获取第一个子节点的孩子节点
- CCLog("%s",nameTag->GetText());
- TiXmlElement* scoreTag=nameTag->NextSiblingElement();//获取孩子节点的兄弟节点(即同级节点,具体含义视获取这个节点的对象而定)
- CCLog("%s",scoreTag->GetText());
-
- firstInfo->name=nameTag->GetText();//得到节点的文本
- firstInfo->score=scoreTag->GetText();
- dictionary->setObject(firstInfo, 0);
-
- for(firstNode=firstNode->NextSiblingElement();firstNode->NextSiblingElement()!=NULL;firstNode=firstNode->NextSiblingElement())//做一个遍历,从第二个子节点到倒数第二个子节点
- {
- index++;
- GradeInfo* firstInfo=GradeInfo::getInstance();
- TiXmlElement* nameTag=firstNode->FirstChildElement();
- TiXmlElement* scoreTag=nameTag->NextSiblingElement();
-
- firstInfo->name=nameTag->GetText();
- CCLog("%s",nameTag->GetText());
- firstInfo->score=scoreTag->GetText();
- CCLog("%s",scoreTag->GetText());
-
- dictionary->setObject(firstInfo, index);
- }
- index++;
- TiXmlNode* lastNode=rootNode->LastChild();//这里再取出最后一个子节点
- TiXmlElement* name=lastNode->FirstChildElement();
- TiXmlElement* score=name->NextSiblingElement();
- GradeInfo* lastInfo=GradeInfo::getInstance();
- lastInfo->name=name->GetText();
- lastInfo->score=score->GetText();
- dictionary->setObject(lastInfo, index);//所有节点信息都保存在字典中
- }
- void XmlParser::scrollViewDidScroll(CCScrollView* view)
- {
-
- }
- void XmlParser::scrollViewDidZoom(CCScrollView* view)
- {
-
- }
- void XmlParser::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
- {
- CCLog("This is number %d",cell->getIdx());
- }
- CCSize XmlParser::cellSizeForTable(CCTableView *table)
- {
- return CCSizeMake(500, 81);
- }
- CCTableViewCell* XmlParser::tableCellAtIndex(CCTableView *table, unsigned int idx)
- {
- CCTableViewCell* viewCell=new CCTableViewCell();
- viewCell->autorelease();
- viewCell->setAnchorPoint(CCPointZero);
- viewCell->setPosition(CCPointZero);
-
- CCSprite* cellSprite=CCSprite::create("cell.png");
- cellSprite->setAnchorPoint(CCPointZero);
- cellSprite->setPosition(CCPointZero);
-
- GradeInfo* infomation=(GradeInfo*)dictionary->objectForKey(idx);
-
- char num[5]="";
- sprintf(num,"%d",idx);
- CCLabelTTF* idxLabel=CCLabelTTF::create(num, "", 25);
- CCLog("%s",num);
- idxLabel->setAnchorPoint(CCPointZero);
- idxLabel->setPosition(ccp(20,cellSprite->getContentSize().height/2));
-
- CCLog("%s",infomation->name.c_str());
- CCLabelTTF* nameLabel=CCLabelTTF::create(infomation->name.c_str(), "", 25);
- nameLabel->setAnchorPoint(CCPointZero);
- nameLabel->setPosition(ccp(cellSprite->getContentSize().width/2,cellSprite->getContentSize().height/2));
-
- CCLog("%s",infomation->score.c_str());
- CCLabelTTF* scoreLabel=CCLabelTTF::create(infomation->score.c_str(), "", 25);
- scoreLabel->setAnchorPoint(CCPointZero);
- scoreLabel->setPosition(ccp(cellSprite->getContentSize().width-70,cellSprite->getContentSize().height/2));
-
- cellSprite->addChild(idxLabel);
- cellSprite->addChild(nameLabel);
- cellSprite->addChild(scoreLabel);
- viewCell->addChild(cellSprite);
-
- return viewCell;
- }
- unsigned int XmlParser::numberOfCellsInTableView(CCTableView *table)
- {
- return 10;
- }