XML字符串的读写操作函数及示例程序

2008-03-24 03:35:37来源: 阅读 ()

新老客户大回馈,云服务器低至5折

以下为引用的内容:

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

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

中国站长.站

 

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

Www_Chinaz_com

 

//---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 Chinaz_com

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

Www^Chinaz^com

 

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();
   }
  }
 }
}
Www^Chinaz^com

 

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);

Chinaz^com


  
   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();
   }
  }
 }
}
Chinaz@com

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:关于 XML 的十种观点

下一篇: .NET中书写XML的一种简单方法