简单的VC 操作XML 文件的的方法

首先建立一个XML文件,我的机器软件configuration如下:
Windows xp professional sp2
IE 6.0( 无关紧要)
VS 2003
------------------------------------------------------------------------------------------------------
#import <msxml3.dll> //I have no idea why msxl4 cannot be used
                         //in my PC, may be something on personal
                         //discrimination
using namespace MSXML2;///the same to above///
-----------------------------------------------------------------------------------创建文件 
int CreateXMLFile(char * xml_file_name)
{   // create an xml file named xml_file_name comtemporily
 // if the return val is "0",it means that creation
 // if failed
 ::CoInitialize(NULL);// this item must be included
                            //it is fatal ,else it will failed
                            //then at last “::CoUninitialize();”
//matched
 
 MSXML2::IXMLDOMDocumentPtr pDoc;
 
 HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument30));//         
//<msxml3.dll> version is 3.0
 
 if(!(SUCCEEDED(hr)))//faild
 {           
         return 0;
 }
 
 MSXML2::IXMLDOMElementPtr pPolicy=pDoc->createElement("Local"); 
 pDoc->appendChild(pPolicy); 
 { // the brackets including 1,2,3 can show the
    //tree structure of the xml file
    // nodes
       //1
         MSXML2::IXMLDOMElementPtr pPolicyType; 
 
         pPolicyType=pDoc->createElement("North");
         pPolicyType->Puttext("Piking");
         pPolicy->appendChild(pPolicyType);
 
         pPolicyType=pDoc->createElement("South");            
         pPolicy->appendChild(pPolicyType);
         {//2.1
                MSXML2::IXMLDOMElementPtr pControlType;
                pControlType=pDoc->createElement("Shanghai");     
                pControlType->Puttext("Xujiahui");
                pPolicyType->appendChild(pControlType);
 
                pControlType.Release();
         }//end 2.1
         pPolicyType=pDoc->createElement("East");
         pPolicy->appendChild(pPolicyType);   
         {//2.2
                MSXML2::IXMLDOMElementPtr pProcessType;
                pProcessType=pDoc->createElement("Heilongjiang");
                pProcessType->Puttext("harbin,Qiqihar");
                pPolicyType->appendChild(pProcessType);
 
                pProcessType=pDoc->createElement("Liaoning");     
                pProcessType->Puttext("ShenYang,Dalian");
                pPolicyType->appendChild(pProcessType);
 
                pProcessType.Release();
         }//end 2.2
         _variant_t varXml(xml_file_name);
         pDoc->save(varXml);    //save the file        
         pPolicy.Release();
         pPolicyType.Release();
 }//end 1
 pDoc.Release();
 ::CoUninitialize();   //necessary it same as new and delete
 return 1;
 
}
 
产生的文件如下。。
<Location>
<North> Pingking</North>
<South>
   <Shanghai>Xujiahui</Shanghai>
</South>
<East>
     <Heilongjiang>harbin,qiqihar</Heilongjiang>
     <Liaoning>Shenyang,Dalian</Liaoning>
</East>
-----------------------------------------------------------------------------------------查找值
访问结点时,如果要读<North>的值,就直接把”North”写进去,返回的就是Pingking了,
要访问<Shanghai>要代入”South\\Shanghai”, 这个和注册表分健值很像
char* GetValueFromXml( char* nodeName)
{// get the value of certain node named nodeName
 // from the xml file
 // the return value 0 represent failure
              char* nodeValue=NULL;
              ::CoInitialize(NULL);
              MSXML2::IXMLDOMDocumentPtr pDoc;
              HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument30));
 
              if(!(SUCCEEDED(hr)))
              {    
                     return nodeValue;
                    
              }  
 
              hr=pDoc->load("book.xml");
              if(!SUCCEEDED(hr))
              {           
                     MSXML2::IXMLDOMElementPtr pPolicyNode=pDoc->GetdocumentElement();           
                     MSXML2::IXMLDOMElementPtr pSelectNode=pPolicyNode->selectSingleNode(nodeName);       
                     nodeValue =    _com_util::ConvertBSTRToString(pSelectNode->text);             
                     pPolicyNode.Release();
                     pSelectNode.Release();
              }
              pDoc.Release();
              ::CoUninitialize();
              return nodeValue;
 
}
--------------------------------------------------------------------------------修改XML 文件中的某一个结点的值
访问方法同上
int modify(const char* nodeName,char* nodeValue)//前者为结点名,后者为结点的字串值。
{// modify the value of node named as nodeName
 // the new value is nodeValue
 // if the xml file is not existed then return
 // 0
       ::CoInitialize(NULL);
       MSXML2::IXMLDOMDocumentPtr pDoc;
       HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument30));
    int rtval=0;
       if(!(SUCCEEDED(hr)))
       {    
              rtval=0;  
              return rtval;
       }  
 
       hr=pDoc->load("book.xml");// 文件名。
      
       if(!SUCCEEDED(hr))
       {
             
              MSXML2::IXMLDOMElementPtr pPolicyNode=pDoc->GetdocumentElement();           
              MSXML2::IXMLDOMElementPtr 二pSelectNode=pPolicyNode->selectSingleNode(nodeName);
              pSelectNode->Puttext(nodeValue);
              pDoc->save("book.xml");
              pPolicyNode.Release();
              pSelectNode.Release();
              rtval=1;         
             
       }
       pDoc.Release();
       ::CoUninitialize();
       return rtval;
      
}
 
-------------------------------------------------------------------------------------------------删去结点

这个就是一个什么remove 的成员函数就搞定了,和查找的差不多,先找到要删的结点,就行了。。当然删后要保存了,这一点不同,想必都知道。。

 

 

posted on 2008-01-30 18:01  施昌权-博客  阅读(2072)  评论(1编辑  收藏  举报

导航