XML字串的讀寫操作函數及樣本程式

來源:互聯網
上載者:User
xml|程式|函數|樣本|字串

// OpXML.cpp : Defines the entry point for the console application.
//

//----------------------- Coded By Ronk --------------------------//
//-----------------------  2005-07-17  --------------------------//

#include "stdafx.h"
#include <stdio.h>

//---You must Setup the MSXML4.0 before using
#import <msxml4.dll>
using namespace MSXML2;

void xmlread(char *ch1,char *ch2,char *ch3);//Read the xmlstr and Get the Text of the Element

void xmlupdate(char *ch1,char *ch2,char *ch3);//Read the xmlstr and Update the Text of the Element

int main(int argc, char* argv[])
{ 
 char xmlstr[1024]="<inesmsg><msghead><version>LZD</version><root>ioio</root></msghead></inesmsg>";
 char strele[1024]="//version//";
 char textstr[1024]="NNYX";
 char value[1024];
 
 printf("The xmlstr is :\n%s\n\n",xmlstr);
 
 xmlread(xmlstr,strele,value); //befor Update
 printf("The Text of Element befor Update is: \n%s\n\n",value);
 
 xmlupdate(xmlstr,strele,textstr);//Change the Text of "<version>" with "NNYX"
 printf("The Updated xmlstr is :\n%s\n\n",xmlstr);
 
 xmlread(xmlstr,strele,value);//after Update
 printf("The Text of Element after Update is :\n%s\n\n",value);
 
 return 0;
}


void xmlread(char* msgstr,char* elestr,char *tex)
{
 //Initialize
 CoInitialize(NULL);
 
 IXMLDOMDocumentPtr pXmlDoc;
 IXMLDOMNodePtr pXmlNode;
 
 HRESULT hr;
 hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
 if (FAILED(hr))
 {
  printf("Faild to Create XMLDom Instance !");
  pXmlDoc=NULL;
 }
 else
 {
  pXmlDoc ->async = VARIANT_FALSE;
  
  //Load xmlmsg
  _bstr_t xmlstr=(LPCSTR)msgstr;
  
  if (!(pXmlDoc->loadXML(msgstr)))
  {
   printf("Failed to Load xmlstr:\n%s\n",(LPCSTR)pXmlDoc->parseError->Getreason());
   pXmlDoc=NULL;
  } 
  else
  {
   //Locate the Element
   _bstr_t str=(LPCSTR)elestr;
   pXmlNode = pXmlDoc ->selectSingleNode(elestr);
   
   if(FAILED(pXmlNode))
    printf("Faild to Locate the Element \n%s\n",str);
   else
   {      
    //Read the Text of Element
    strcpy(tex,(LPCSTR)pXmlNode ->text);
    
    
    //Realese
    pXmlDoc.Release();
    pXmlNode.Release();
   }
  }
 }
}

void xmlupdate(char *msgstr,char *elestr,char *upstr)
{
 //---Initialize
 CoInitialize(NULL);
 
 IXMLDOMDocumentPtr pXmlDoc;
 IXMLDOMNodePtr pXmlNode;
 
 HRESULT hr;
 hr = pXmlDoc.CreateInstance(__uuidof(DOMDocument));
 
 if (FAILED(hr))
 {
  printf("Faild to Create XMLDom Instance !");
  pXmlDoc=NULL;
 }
 else
 {
  pXmlDoc ->async = VARIANT_FALSE;
  
  //Load xml str
  _bstr_t xmlstr=(LPCSTR)msgstr;
  
  if (!(pXmlDoc->loadXML(msgstr)))
  {  
   printf("Failed to load xmlstr:\n%s\n",(LPCSTR)pXmlDoc->parseError->Getreason());
   pXmlDoc=NULL;
  }
  else
  {   
   //Located the Element
   _bstr_t str=(LPCSTR)elestr;
   pXmlNode = pXmlDoc ->selectSingleNode(elestr);
   
   if (FAILED(pXmlNode))
    printf("Failed to Locate the Element \n%s\n",str);
   else
   {    
    //Update the Text of the Element
    pXmlNode->text = upstr;
    
    //Get the New xmlstr
    strcpy(msgstr,(LPCSTR)pXmlDoc->xml);
    
    //Realese
    pXmlDoc.Release();
    pXmlNode.Release();
   }
  }
 }
}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.