当前位置:首页>网络学院>程序开发>XML教程>文章内容

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

[ 来源:www.it55.com | 作者: | 时间:2007-07-14 | 收藏 | 推荐 ] 【

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

it55.com

//----------------------- Coded By Ronk --------------------------//
//-----------------------  2005-07-17  --------------------------// http://www.it55.com/

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

www.it55.com

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

it55.com

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

www.it55.com

void xmlupdate(char *ch1,char *ch2,char *ch3);//Read the xmlstr and Update the Text of the Element www.it55.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;
}

vd;k;l www.it55.com rdfg


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

it55.com


   
   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.it55.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); 45398 www.it55.com it55学习IT知识,享受IT生活 4dfkjn
   
   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();
   }
  }
 }
} www.it55.com在线教程

(编辑:IT资讯之家 www.it55.com

返回顶部
 

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]

图片文章