学习笔记之cocos2d-x2.1.1实现修改plist文件数据,用TinyXml解析XML

如果不了解TinyXml,建议先看一下这篇博客

http://blog.csdn.net/nat_myron/article/details/8740028

plist文件如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>picture</key>
        <array>
            <string>1.png</string>
            <string>2.png</string>
            <string>3.png</string>
        </array>
        <key>point</key>
        <array>
            <string>{257.902405,410.831543}</string>
            <string>{521.964294,436.794006}</string>
            <string>{766.067932,410.830719}</string>
        </array>
    </dict>
</plist>

操作如下

	TiXmlDocument *myDocument = new TiXmlDocument("SmallTools.plist");
	myDocument->LoadFile();
	TiXmlElement *RootPlist  = myDocument->RootElement();//plist
	TiXmlElement *FirstDict = RootPlist->FirstChildElement();//dict
	TiXmlElement *FirstKey = FirstDict->FirstChildElement();//key
	TiXmlElement *SecDict = FirstKey->NextSiblingElement();//array
	TiXmlElement *SecKey = SecDict->NextSiblingElement();//key
	TiXmlElement *ThirdDict = SecKey->NextSiblingElement();//array
	CCLOG(ThirdDict->Value());
	//删除所有的子结点
	ThirdDict->Clear();
	for (int i=0;i<TagCount;i++)
	{
		CCSprite* sprite = (CCSprite*)sprite_array->objectAtIndex(i);
		CCPoint point = sprite->getPosition();
		char str[50];
		sprintf(str,"{%lf,%lf}",point.x,point.y);
		TiXmlElement *NameElement = new TiXmlElement("string");  
		ThirdDict->LinkEndChild(NameElement);  
		TiXmlText *NameContent = new TiXmlText(str);  
		NameElement->LinkEndChild(NameContent);  
		myDocument->SaveFile("SmallTools.plist");  
	}

包含头文件

#include "tinystr.h" 
#include "tinyxml.h"


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值