第一步:node
先要在TinyXml增長函數 bool TiXmlDocument::LoadMemory( const char * pBuff, int length, TiXmlEncoding encoding )android
/// Load Xml form memory buff. Returns true if successful. bool TiXmlDocument::LoadMemory( const char * pBuff, int length, TiXmlEncoding encoding ) { if ( !pBuff || length == 0 ) { SetError( TIXML_ERROR, 0, 0, TIXML_ENCODING_UNKNOWN ); return false; } // If we have a file, assume it is all one big XML file, and read it in. // The document parser may decide the document ends sooner than the entire file, however. TIXML_STRING data; data.reserve( length ); char* buf = new char[ length+1 ]; buf[0] = 0; memcpy( buf, pBuff, length ); const char* lastPos = buf ; const char* p = buf ; buf[length] = 0; while( *p ) { assert( p < (buf+length) ); if ( *p == 0xa ) { // Newline character. No special rules for this. Append all the characters // since the last string, and include the newline. data.append( lastPos, (p-lastPos+1) ); // append, include the newline ++p; // move past the newline lastPos = p; // and point to the new buffer (may be 0) assert( p <= (buf+length) ); } else if ( *p == 0xd ) { // Carriage return. Append what we have so far, then // handle moving forward in the buffer. if ( (p-lastPos) > 0 ) { data.append( lastPos, p-lastPos ); // do not add the CR } data += (char)0xa; // a proper newline if ( *(p+1) == 0xa ) { // Carriage return - new line sequence p += 2; lastPos = p; assert( p <= (buf+length) ); } else { // it was followed by something else...that is presumably characters again. ++p; lastPos = p; assert( p <= (buf+length) ); } } else { ++p; } } // Handle any left over characters. if ( p-lastPos ) { data.append( lastPos, p-lastPos ); } delete [] buf; buf = 0; Parse( data.c_str(), 0, encoding ); if ( Error() ) { return false; } return true; }
第二步驟 調用ios
void readXml() { string documentPath = "heroList.xml"; TiXmlDocument *myDocument = new TiXmlDocument(documentPath.c_str());//"WriteTest.xml"); if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) { /*由於android沒法讀取assets下的xml數據文件,ios一樣存在讀取問題,這個具體緣由是由於apk和app實質上是一個壓縮包, 壓縮包的內容讀取不一樣於通常的文件讀取,由於文件已經被壓縮過了, 因此採用了讀取buffer的方法,經過將buffer交給LoadMemory解析,初始化TiXmlDocument *myDocument 中的數據。 */ unsigned long nLength = 0; char* pBuff = (char*)cocos2d::CCFileUtils::sharedFileUtils()->getFileData(CCFileUtils::sharedFileUtils()->fullPathForFilename(documentPath.c_str()).c_str(),"rt", &nLength ); myDocument->LoadMemory( pBuff, nLength); } else { myDocument->LoadFile(); } TiXmlElement *RootElement = myDocument->RootElement();/*讀取根節點*/ if(RootElement == NULL) { //LD("read from data xml error!"); } TiXmlElement *FirstPerson = RootElement->FirstChildElement();/*讀取根節點中的第一個節點數據*/ while (FirstPerson) { CCLog("node name = %s",FirstPerson->Value()); TiXmlAttribute *idAttribute = FirstPerson->FirstAttribute();/*個人文件中有兩個屬性值,一個是id屬性*/ TiXmlAttribute *decAttribute = idAttribute->Next();/*個人文件中有兩個屬性值,一個是描述屬性*/ CCLog("attrName = %s,attrValue = %d",idAttribute->Name(),atoi(idAttribute->Value()));/*打印出屬性名稱和屬性值*/ CCLog("attrName = %s,attrValue = %s",decAttribute->Name(),decAttribute->Value()); TiXmlElement *firstChild = FirstPerson->FirstChildElement();/*讀取第一個節點的第一個子節點*/ map<string,int> tempMap; while(firstChild) { /*讀取第一個節點的數據*/ CCLog("childName = %s,childValue = %d",firstChild->Value(),atoi(firstChild->GetText())); tempMap.insert(map<string, int>::value_type (firstChild->Value(), atoi(firstChild->GetText()))); firstChild = firstChild->NextSiblingElement();/*讀取完第一個節點的數據,firstChild指針向後移位,讀取下一個節點的數據*/ string childName = string(firstChild->Value()); if ( childName == "boundRidus") { break; } } while(firstChild) { /*我自定義的"heroList.xml"中,本子節點有三個屬性,依次讀取三個屬性,詳細的數據已經註釋在代碼中了*/ TiXmlAttribute *posXAttribute = firstChild->FirstAttribute(); TiXmlAttribute *posYAttribute = posXAttribute->Next(); TiXmlAttribute *radiusAttr = posYAttribute->Next(); CCLog("attriName = %s,attriValue = %d",posXAttribute->Name(),atoi(posXAttribute->Value())); firstChild = firstChild->NextSiblingElement(); } //m_heroDatas.push_back(hData); FirstPerson = FirstPerson->NextSiblingElement(); } writeLevel(1); writeXml(); } void writeLevel(int level) { /*生成xml文件,這個儘可能不要在正式的程序中使用,生成的xml數據只是爲了方便配置數據的人員方便配置*/ const char * attribut[12] = {"mId","aId","pId","pAId","posX","posY","mType","explodeEffect","data8","data9","data10","data11"}; TiXmlDocument doc ; TiXmlDeclaration *declare =new TiXmlDeclaration("1.0" , "",""); doc.LinkEndChild(declare); doc.LinkEndChild(new TiXmlComment("levelDataInf")); TiXmlElement *root = new TiXmlElement("levelDataConfig"); for (int i=0;i!=4;++i) { TiXmlElement *sub = new TiXmlElement("createMonsters"); sub->SetAttribute("order" , i); // 向sub中添加屬性 for(int enemyC=0;enemyC!=7;++enemyC) { TiXmlElement *monster = new TiXmlElement("monster"); for (int j=0;j!=12;++j) { monster->SetAttribute(attribut[j] , j); } // 向sub中添加屬性 sub->LinkEndChild(monster); // 將child追加到sub中,以做爲子元素 } root->LinkEndChild(sub); // 將sub } doc.LinkEndChild(root); string documentPath = "levelTest.xml"; doc.SaveFile(documentPath.c_str()); } void writeXml() { /*寫文件,若是程序中使用了xml的進行讀寫數據,可使用這個來從新寫入遊戲配置數據,不過本人儘可能推薦使用CCUserDefault來作*/ TiXmlDocument doc ; TiXmlDeclaration *declare =new TiXmlDeclaration("1.0" , "",""); doc.LinkEndChild(declare); doc.LinkEndChild(new TiXmlComment("personalInfo")); TiXmlElement *personalInfo = new TiXmlElement("personalInfo"); doc.LinkEndChild(personalInfo); personalInfo->SetAttribute("userId", 0); personalInfo->SetAttribute("loginTime", 0); personalInfo->SetAttribute("url", 0); personalInfo->SetAttribute("scoreRecord", 0); personalInfo->SetAttribute("coinTotal", 0); personalInfo->SetAttribute("diamondTotal", 0); personalInfo->SetAttribute("character", 0); personalInfo->SetAttribute("knife",0); personalInfo->SetAttribute("sprint", 0); personalInfo->SetAttribute("protectCover", 9); personalInfo->SetAttribute("currentLevel",9); string documentPath = CCFileUtils::sharedFileUtils()->getWritablePath()+"writeData.xml"; /*此路徑win 在../proj.win32/Debug.win32,android在/data/data/包名/,若是是ios在*/ doc.SaveFile(documentPath.c_str()); } 下面介紹TinyXML的一些類。在TinyXML中,根據XML的各類元素來定義了一些類: TiXmlBase:整個TinyXML模型的基類。 TiXmlAttribute:對應於XML中的元素的屬性。 TiXmlNode:對應於DOM結構中的節點。 TiXmlComment:對應於XML中的註釋 TiXmlDeclaration:對應於XML中的申明部分,<?versiong="1.0" ?>。 TiXmlDocument:對應於XML的整個文檔。 TiXmlElement:對應於XML的元素。 TiXmlText:對應於XML的文字部分 TiXmlUnknown:對應於XML的未知部分。 TiXmlHandler:定義了針對XML的一些操做。