1、XML文件读取
void BB:: File()
{
//从app中读取文件(本项目)
std::string path=CCFileUtils::sharedFileUtils()->getWritablePath();
std::string xmlPath=path+std::string("data.xml");
std::cout<<xmlPath<<std::endl;
//从沙盒中读取xml文件
std::string path2=CCFileUtils::sharedFileUtils()->fullPathForFilename("data.xml");
std::cout<<path2<<std::endl;
if(!CCFileUtils::sharedFileUtils()->isFileExist(xmlPath))//如果沙盒 文件不存在,拷贝文件
{
//拷贝文件
constchar*file_path=CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile(path2.c_str(),"");
unsigned long size;
char *pFileContent=(char *)CCFileUtils::sharedFileUtils()->getFileData(file_path,"r", &size);
CCLog("%s",pFileContent);
//打开文件
FILE *file=fopen(xmlPath.c_str(),"w");
if(file)
{
//写入内容
fputs(pFileContent, file);
fclose(file);
}
}
2、封装文件
//对操作文件的封装 方法一:
bool BB::copyFile(std::string sourcePath,std::string destinationPath)
{
if(!CCFileUtils::sharedFileUtils()->isFileExist(sourcePath))//如果沙盒 文件不存在,拷贝文件
{
const char*file_path=CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile(destinationPath.c_str(),"");
unsigned long size;
char *pFileContent=(char *)CCFileUtils::sharedFileUtils()->getFileData(file_path, "r", &size);
CCLog("%s",pFileContent);
FILE *file=fopen(sourcePath.c_str(), "w");
if(file)
{
fputs(pFileContent, file);
fclose(file);
}
}
return true;
}
//对操作文件的封装 方法二:
bool BB::copyFile1(const char* sourcePath,const char* destinationPath)
{
CCLog("%s",sourcePath);
CCLog("%s",destinationPath);
if(!CCFileUtils::sharedFileUtils()->isFileExist(sourcePath))//如果沙盒 文件不存在,拷贝文件
{
const char*file_path=CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile(destinationPath,"");
unsigned long size;
char *pFileContent=(char *)CCFileUtils::sharedFileUtils()->getFileData(file_path, "r", &size);
CCLog("%s",pFileContent);
FILE *file=fopen(sourcePath, "w");
if(file)
{
fputs(pFileContent, file);
fclose(file);
}
}
return true;
}
3、 XML文件解析操作
void BB::xmlParse()
{
//
//文件可写
std::string path=CCFileUtils::sharedFileUtils()->getWritablePath();
std::string xmlPath=path+std::string("Chapters.xml");
//获得到目录
TiXmlDocument *myDocument=new TiXmlDocument(xmlPath.c_str());
myDocument->LoadFile();//加载文件
CCLOG("fgwaerfgierwaiugvgfguvyguyv");
//读取根节点 chapters
TiXmlElement *RootElement=myDocument->RootElement();
//打印 键、值
CCLog("%s",RootElement->Value());
CCLog("%s",RootElement->GetText());
//读取根节点下的第一个节点 chapter
TiXmlElement *chapterElement=RootElement->FirstChildElement();
CCLog("%s",chapterElement->Value());
CCLog("%s",chapterElement->GetText());
//第一组数据
CCLOG("第一组数据:---------------------------");
// TiXmlElement *element=chapterElement->NextSiblingElement();
// CCLog("%s",element->Value());
// CCLog("%s",element->GetText());
//
TiXmlElement *nameElement=chapterElement->FirstChildElement();
CCLog("%s",nameElement->Value());
CCLog("%s",nameElement->GetText());
TiXmlElement *numElement=nameElement->NextSiblingElement();
CCLog("%s",numElement->Value());
CCLog("%s",numElement->GetText());
TiXmlElement *nameElement1=numElement->NextSiblingElement();
CCLog("%s",nameElement1->Value());
CCLog("%s",nameElement1->GetText());
///
//第二组数据
CCLOG("第二组数据:---------------------------");
TiXmlElement *nameElement2=chapterElement->NextSiblingElement();
CCLog("%s",nameElement2->Value());
CCLog("%s",nameElement2->GetText());
TiXmlElement *nameElement21=nameElement2->FirstChildElement();
CCLog("%s",nameElement21->Value());
CCLog("%s",nameElement->GetText());
TiXmlElement *numElement22=nameElement21->NextSiblingElement();
CCLog("%s",numElement22->Value());
CCLog("%s",numElement22->GetText());
TiXmlElement *nameElement23=numElement22->NextSiblingElement();
CCLog("%s",nameElement23->Value());
CCLog("%s",nameElement23->GetText());
//第三组数据
CCLOG("第三组数据:---------------------------");
TiXmlElement *nameElement3=nameElement2->NextSiblingElement();
CCLog("%s",nameElement3->Value());
CCLog("%s",nameElement3->GetText());
TiXmlElement *nameElement31=nameElement3->FirstChildElement();
CCLog("%s",nameElement31->Value());
CCLog("%s",nameElement31->GetText());
TiXmlElement *numElement32=nameElement31->NextSiblingElement();
CCLog("%s",numElement32->Value());
CCLog("%s",numElement32->GetText());
TiXmlElement *nameElement33=numElement32->NextSiblingElement();
CCLog("%s",nameElement33->Value());
CCLog("%s",nameElement33->GetText());
char *a=(char *)nameElement31->GetText();
int b=atoi(numElement32->GetText());
int c=atoi(nameElement33->GetText());
chapter->chapterArray(a,b,c);
array->addObject(chapter);
// // for(int i=0;i<array->count();i++)
// {
// CCLog( "%s",(char *)array->objectAtIndex(0) );
// CCLog( "%d",(int )array->objectAtIndex(1) );
// }
/
// //修改节点,没有GetText()方法
//
TiXmlNode *oldNode=numElement->FirstChild();//第一组的第二个元素
CCLog("%s",oldNode->Value());
//
// const char *ver=oldNode->Value();
// int oldVer=atoi(ver);将const char *转化为 int类型
// char text[10];
// sprintf(text, "value%i",oldVer+1);
// std::cout<<text<<std::endl;
//
// TiXmlText newText(text);
// numElement->ReplaceChild(oldNode, newText);
// //注意这里要用父类的节点调用,否则会不起作用
// myDocument->SaveFile();//这个将修改的保存到文件
//
// //添加节点,需要使用其父节点调用,若使用同等级的节点,则会作为其子节点添加
// TiXmlElement *Broadcast_pid=new TiXmlElement("age");
// TiXmlText *text1=new TiXmlText("11111");
// Broadcast_pid->SetAttribute("info", "the pid");
// Broadcast_pid->LinkEndChild(text1);
// chapterElement->LinkEndChild(Broadcast_pid);//最后把这个节点又加上了
// myDocument->SaveFile();
// CCLog("qqqqqqqqqqqqqq");
//
// //移除节点
// TiXmlElement *nameNode=chapterElement->FirstChildElement();
// chapterElement->RemoveChild(nameNode);
// myDocument->SaveFile();
}
注意:
CCLog里面封装了NSLog,因为它只在debug时候调用,在release时不调用。