用tinyxml 库创建并读写xml代码截取

因为自己每次写完之后都忘记了  然后又要从别的地方学习再重新写  还不如记录再这里

//创建
        tixmlDocument *pXmlDocument = new tixmlDocument(m_strFilePath.c_str());//
        tixmlDeclaration *pDeclaretion = new tixmlDeclaration("1.0","UTF-8","");//创建XML声明
        pXmlDocument->LinkEndChild(pDeclaretion);
        tixmlElement *pXmlElement = new tixmlElement("Symbol");
        pXmlDocument->LinkEndChild(pXmlElement);    
        pXmlDocument->SaveFile(m_strFilePath.c_str());

//读
tixmlDocument *pXmlDoc = new tixmlDocument();
 pXmlDoc->LoadFile(strFilePath.c_str());

 tixmlElement *pRootElement = pXmlDoc->RootElement(); 
 if (pRootElement == NULL)
 {
  return;
 }
 // 第一个子节点 
 tixmlElement *pSignNode = pRootElement->FirstChildElement("sign"); 
 if (!pSignNode)
 {
  SAFE_DELETE(pSignNode);
  return;
 }
 while (pSignNode != NULL)
 {
  CMapSingleParameter *pMapSingleParameter = new CMapSingleParameter();
  if (!pMapSingleParameter)
  {
   SAFE_DELETE(pMapSingleParameter);
   return ;
  }
  // text
  TCHAR tcharValue[MAX_PATH * 4] = {_T('\0')};

  // name
  const CHAR *pszName = pSignNode->Attribute("name");
  if (pszName != NULL)
  {
   CommonUtil::UTF8ToTCHAR(pszName,tcharValue);
   pMapSingleParameter->AppendParameter(_T("name"),tcharValue);
  }
  
  // height
  const CHAR *pszHeight = pSignNode->Attribute("height");
  if (pszHeight != NULL)
  {
   CommonUtil::UTF8ToTCHAR(pszHeight,tcharValue);
   pMapSingleParameter->AppendParameter(_T("height"),tcharValue);
  }
  //Content
  const CHAR *pszContent = pSignNode->GetText();
  if (pszContent != NULL)
  {
   CommonUtil::UTF8ToTCHAR(pszContent,tcharValue);
   pMapSingleParameter->AppendParameter(_T("content"),tcharValue);
  }

  m_pLatelyVectorSymbol->push_back(pMapSingleParameter);  
  pSignNode = pSignNode->NextSiblingElement();   
 } 



//写
string strFile = m_strFilePath;
 
 //创建文档对象
 tixmlDocument myXmlDocument;
 //加载文件数据 
 myXmlDocument.LoadFile(strFile.c_str());

 tixmlElement *pRootElement = myXmlDocument.RootElement(); 
 if (pRootElement == NULL)
 {
  return ;
 }

 if (pRootElement != NULL)
 {
  tixmlElement *pFirstNode = pRootElement->FirstChildElement("sign");
  if (pFirstNode == NULL)
  { 
   int nSignPos = 0;

   for (TVectorSymbol::iterator ite = m_pLatelyVectorSymbol->begin(); ite != m_pLatelyVectorSymbol->end(); ++ite)
   {
    CMapSingleParameter *pMapSingleParameter = *ite;
    tixmlElement *insertElement = new tixmlElement("sign");
    pRootElement->LinkEndChild(insertElement);
    xstring strUnicode = pMapSingleParameter->GetParameterValue(_T("name"));
    TCHAR tcharValue[MAX_PATH*4]= {_T('\0')};
    _tcscpy(tcharValue,strUnicode.c_str());
    char chUtf8[MAX_PATH*4] = {'\0'};
    CommonUtil::TCHARToUTF8(tcharValue,chUtf8);
    insertElement->SetAttribute("name",chUtf8);


    strUnicode = pMapSingleParameter->GetParameterValue(_T("height"));
    _tcscpy(tcharValue,strUnicode.c_str());
    CommonUtil::TCHARToUTF8(tcharValue,chUtf8);
    insertElement->SetAttribute("height",chUtf8);

    strUnicode = pMapSingleParameter->GetParameterValue(_T("content"));
    _tcscpy(tcharValue,chUtf8);
    
    tixmlText *pXmlText = new tixmlText(chUtf8);
    pXmlText->SetCDATA(true);//添加格式化<![CDATA[]]>
    
    insertElement->LinkEndChild(pXmlText);

    m_nLatelyTotal ++; 
   }  
  }   
 } 
 myXmlDocument.SaveFile(strFile.c_str());
}


//删除节点

 string strFile = m_strFilePath;

 //创建文档对象
 tixmlDocument myXmlDocument;
 //加载文件数据 
 myXmlDocument.LoadFile(strFile.c_str());

 tixmlElement *pRootElement = myXmlDocument.RootElement(); 
 if (pRootElement == NULL)
 {
  return ;
 }

 tixmlElement *pSignNode = pRootElement->FirstChildElement("sign");
 while (pSignNode != NULL)
 {
  m_nLatelyTotal --;
  tixmlElement *pSignRemoveNode = pSignNode;
  pSignNode= pSignNode->NextSiblingElement("sign");
  pRootElement->RemoveChild(pSignRemoveNode);  
 }

 myXmlDocument.SaveFile(strFile.c_str());

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念