cocos2d-x解析xml文件

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上显示,具体看代码。

代码如下:
  1. bool XmlParser::init()
  2. {
  3.     if(!CCLayer::init())
  4.     {
  5.         return false;
  6.     }
  7.     dictionary=CCDictionary::create();
  8.     dictionary->retain();
  9.     size=CCDirector::sharedDirector()->getWinSize();
  10.     CCSprite* background=CCSprite::create("background.jpg");
  11.     background->setPosition(ccp(size.width*0.5,size.height*0.5));
  12.     this->addChild(background);
  13.    
  14.     parseXml();
  15.    
  16.     CCTableView* viewLayer=CCTableView::create(this, CCSizeMake(430, 600));
  17.     viewLayer->setAnchorPoint(ccp(0.5,0.5));
  18.     viewLayer->setDirection(kCCScrollViewDirectionVertical);
  19.     viewLayer->setVerticalFillOrder(kCCTableViewFillTopDown);
  20.     viewLayer->setPosition(ccp(background->getContentSize().width/2-220,background->getContentSize().height/2-400));
  21.     viewLayer->setContentOffset(ccp(0, 600-viewLayer->getContentSize().height));
  22.     viewLayer->setDelegate(this);
  23.     this->addChild(viewLayer);
  24.    
  25.     return  true;
  26. }

  27. void XmlParser::parseXml()//解析xml
  28. {
  29.     int index=0;
  30.     GradeInfo* firstInfo=GradeInfo::getInstance();
  31.    
  32. std::string xmlFile=CCFileUtils::sharedFileUtils()->fullPathForFilename("perinfo.xml");
  33. TiXmlDocument* xmlData=new TiXmlDocument(xmlFile.c_str());//初始化一个xml文档对象
  34. xmlData->LoadFile();
  35.    
  36.     TiXmlElement* rootNode=xmlData->RootElement();//每个xml文件对应一个唯一根节点,这里获取根节点。
  37.     CCLog("%s",rootNode->Value());
  38.    
  39.     TiXmlElement* firstNode=rootNode->FirstChildElement();//获取根节点的第一个子节点
  40.     TiXmlElement* nameTag=firstNode->FirstChildElement();//获取第一个子节点的孩子节点
  41.     CCLog("%s",nameTag->GetText());
  42.     TiXmlElement* scoreTag=nameTag->NextSiblingElement();//获取孩子节点的兄弟节点(即同级节点,具体含义视获取这个节点的对象而定)
  43.     CCLog("%s",scoreTag->GetText());
  44.    
  45.     firstInfo->name=nameTag->GetText();//得到节点的文本
  46.     firstInfo->score=scoreTag->GetText();
  47.     dictionary->setObject(firstInfo, 0);
  48.    
  49.     for(firstNode=firstNode->NextSiblingElement();firstNode->NextSiblingElement()!=NULL;firstNode=firstNode->NextSiblingElement())//做一个遍历,从第二个子节点到倒数第二个子节点
  50.     {
  51.         index++;
  52.         GradeInfo* firstInfo=GradeInfo::getInstance();
  53.         TiXmlElement* nameTag=firstNode->FirstChildElement();
  54.         TiXmlElement* scoreTag=nameTag->NextSiblingElement();
  55.        
  56.         firstInfo->name=nameTag->GetText();
  57.         CCLog("%s",nameTag->GetText());
  58.         firstInfo->score=scoreTag->GetText();
  59.         CCLog("%s",scoreTag->GetText());
  60.        
  61.         dictionary->setObject(firstInfo, index);
  62.     }
  63.     index++;
  64.     TiXmlNode* lastNode=rootNode->LastChild();//这里再取出最后一个子节点
  65.     TiXmlElement* name=lastNode->FirstChildElement();
  66.     TiXmlElement* score=name->NextSiblingElement();
  67.     GradeInfo* lastInfo=GradeInfo::getInstance();
  68.     lastInfo->name=name->GetText();
  69.     lastInfo->score=score->GetText();
  70.     dictionary->setObject(lastInfo, index);//所有节点信息都保存在字典中
  71. }

  72. void XmlParser::scrollViewDidScroll(CCScrollView* view)
  73. {
  74.    
  75. }

  76. void XmlParser::scrollViewDidZoom(CCScrollView* view)
  77. {
  78.    
  79. }

  80. void XmlParser::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
  81. {
  82.     CCLog("This is number %d",cell->getIdx());
  83. }

  84. CCSize XmlParser::cellSizeForTable(CCTableView *table)
  85. {
  86.     return CCSizeMake(500, 81);
  87. }

  88. CCTableViewCell* XmlParser::tableCellAtIndex(CCTableView *table, unsigned int idx)
  89. {
  90.     CCTableViewCell* viewCell=new CCTableViewCell();
  91.     viewCell->autorelease();
  92.     viewCell->setAnchorPoint(CCPointZero);
  93.     viewCell->setPosition(CCPointZero);
  94.    
  95.     CCSprite* cellSprite=CCSprite::create("cell.png");
  96.     cellSprite->setAnchorPoint(CCPointZero);
  97.     cellSprite->setPosition(CCPointZero);
  98.        
  99.     GradeInfo* infomation=(GradeInfo*)dictionary->objectForKey(idx);
  100.    
  101.     char num[5]="";
  102.     sprintf(num,"%d",idx);
  103.     CCLabelTTF* idxLabel=CCLabelTTF::create(num, "", 25);
  104.     CCLog("%s",num);
  105.     idxLabel->setAnchorPoint(CCPointZero);
  106.     idxLabel->setPosition(ccp(20,cellSprite->getContentSize().height/2));
  107.    
  108.     CCLog("%s",infomation->name.c_str());
  109.     CCLabelTTF* nameLabel=CCLabelTTF::create(infomation->name.c_str(), "", 25);
  110.     nameLabel->setAnchorPoint(CCPointZero);
  111.     nameLabel->setPosition(ccp(cellSprite->getContentSize().width/2,cellSprite->getContentSize().height/2));
  112.    
  113.     CCLog("%s",infomation->score.c_str());
  114.     CCLabelTTF* scoreLabel=CCLabelTTF::create(infomation->score.c_str(), "", 25);
  115.     scoreLabel->setAnchorPoint(CCPointZero);
  116.     scoreLabel->setPosition(ccp(cellSprite->getContentSize().width-70,cellSprite->getContentSize().height/2));
  117.    
  118.     cellSprite->addChild(idxLabel);
  119.     cellSprite->addChild(nameLabel);
  120.     cellSprite->addChild(scoreLabel);
  121.     viewCell->addChild(cellSprite);
  122.    
  123.     return viewCell;
  124. }

  125. unsigned int XmlParser::numberOfCellsInTableView(CCTableView *table)
  126. {
  127.     return 10;
  128. }
复制代码
运行效果如下:

11.png (50.58 KB, 下载次数: 31)

下载附件 保存到相册

1 小时前 上传


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值