xml解析(tinyXml使用)

#include "myXml.h"
#include
#include
#include "tinyxml.h"

using namespace std;
//用下列字符串作爲xml文件的初始內容:


int CMyXml::createXmlFile(const char* cPathName, const char* sXmlContent, int iEncoding)
{

//創建doc對象
 TiXmlDocument doc( cPathName);
 
 //TIXML_ENCODING_UNKNOWN =0,
 //TIXML_ENCODING_UTF8,
 //TIXML_ENCODING_LEGACY
 string sUTF8 = "";
 if( iEncoding == 1 )
 {

  //函數codeConvert是將sXmlContent字符串從當前的代碼頁轉換爲UTF8編碼的字符串。
  sUTF8 = codeConvert( sXmlContent, ::GetACP(), CP_UTF8 );
 }
 else
 {
  sUTF8 = sXmlContent;
 }
 //在內存中生成xml樹
 doc.Parse( sUTF8.c_str(),0,TiXmlEncoding(iEncoding) );
 if ( doc.Error() )
 {
  return -1;
 }

//保存爲文件
 doc.SaveFile();
 return 0;
}

//2,加載文件到內存。讀取xml。

//接下來可以使用doc對xml文件進行修改,讀取等操作。

//3,插入新節點

//向xml文件中插入新的節點,cPathName爲文件路徑,cElmentName爲要插入的節點的名稱。
int CMyXml::insertNewElement(const char *cPathName,const char* rootNodeNane,const char* cElementName)
{

//首先加載xml文件
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile(cPathName);

 if ( !loadOkay )
 {
  return -1;
 }
 TiXmlNode* tRootNode = 0;

//找到root節點
 tRootNode = doc.FirstChild(rootNodeNane);

//創建一個新的節點cElementName
 TiXmlElement * tNewElement = new TiXmlElement( cElementName );
 TiXmlText tiInsertText( cElementName );
 tNewElement->InsertEndChild( tiInsertText );

 //創建一個新的節點
 //TiXmlElement * tNewElement = new TiXmlElement(cElementName);
// tNewElement->SetAttribute( "name", cElementName );
// tNewElement->SetAttribute( "status", "0" );


//將新創建的節點插入爲root節點的孩子節點,而且爲最後一個孩子節點。參數tRootNode爲將要插入的節點的父節點。
 //LinkEndChild最好傳入new出來的對象,否則會出現_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)問題
tRootNode->LinkEndChild(tNewElement);//tChildNode = toElement->InsertEndChild( tRootNode );

//保存文件。
 doc.SaveFile();
 return 0;
}

//4,插入新的text節點。0這類的節點稱爲text節點,和這樣的節點有點區別

int CMyXml::setTextNodeText(const char *cPathName, const char *cText)
{
 TiXmlDocument doc( cPathName);
 bool loadOkay = doc.LoadFile(cPathName);

 if ( !loadOkay )
 {
  return -1;
 }
 TiXmlNode* tRootNode = 0;
 TiXmlNode* tChildNode = 0;
 TiXmlElement* todoElement = 0;

//找到根節點
 tRootNode = doc.FirstChild( _T("document") );
 assert( tRootNode );

//找根節點的第一個孩子節點
 //tChildNode = tRootNode->FirstChild( _T("good") );
 tChildNode = tRootNode->FirstChild();
 assert(tChildNode);

//創建新的節點

 TiXmlElement tiInsertNode( _T("good") );
 TiXmlText tiInsertText( cText );
 tiInsertNode.InsertEndChild( tiInsertText );

//插入
 tRootNode->ReplaceChild( tChildNode, tiInsertNode );

//保存
 doc.SaveFile();
 return 0;
}

//5,讀取xml內容。將xml文件的內容導出爲字符串。
int CMyXml::getXmlContent(const char *cPathName, string &sContent)
{
 TiXmlDocument doc;
 bool loadOkay = doc.LoadFile(cPathName);
 if ( !loadOkay )
 {
  return -1;
 }

 TiXmlPrinter printer;
 doc.Accept(&printer);
 //sContent = printer.CStr();
 sContent = printer.Str();//使用此函數時注意在預編譯器中添加TIXML_USE_STL
//從根節點開始,以節點爲單位導出

//然後便利所有的兄弟節點。
 return 0;
}

 

//轉碼
//獲取當前的代碼頁,通過當前的代碼頁(sourceCodepage)轉成目標編碼(targetCodepage)的數據
string CMyXml::codeConvert( const string& str,  int  sourceCodepage,  int  targetCodepage)
{
    int  len = 0;
   
    len = str.length();
   
    int  unicodeLen = ::MultiByteToWideChar(sourceCodepage,0,str.c_str(),-1,NULL,0); 
   
    wchar_t *  pUnicode; 
    pUnicode = new  wchar_t[unicodeLen+1]; 
   
    memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
       
    ::MultiByteToWideChar(sourceCodepage,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen); 
   
    BYTE  *  pTargetData = NULL; 
    int  targetLen = 0;
    targetLen = ::WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char  *)pTargetData,0,NULL,NULL); 
   
    pTargetData=new  BYTE[targetLen+1]; 
    memset(pTargetData,0,targetLen+1); 
   
    ::WideCharToMultiByte(targetCodepage,0,(LPWSTR)pUnicode,-1,(char  *)pTargetData,targetLen,NULL,NULL); 
   
    string  rt; 
    rt = (char*)pTargetData; 
   
    delete  pUnicode; 
    delete  pTargetData;

    return  rt; 
}

 

#ifndef _CMYXML_H
#define _CMYXML_H

#include
#include

 

using namespace std;
class CMyXml
{
public:
 int createXmlFile(const char* cPathName, const char* sXmlContent, int iEncoding);
 int insertNewElement(const char *cPathName,const char* rootNodeNane,const char* cElementName);
 int setTextNodeText(const char *cPathName, const char *cText);
 int getXmlContent(const char *cPathName, string &sContent);
 string codeConvert( const string& str,  int  sourceCodepage,  int  targetCodepage);
};
#endif

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章