·您的位置: 首页 » 资源教程 » 编程开发 » HTML、CSS » XML字符串的读写操作函数及示例程序

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

类别: XML教程  评论数:0 总得分:0

// 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%snn",xmlstr);
 
 xmlread(xmlstr,strele,value); //befor Update
 printf("The Text of Element befor Update is: n%snn",value);
 
 xmlupdate(xmlstr,strele,textstr);//Change the Text of "<version>" with "NNYX"
 printf("The Updated xmlstr is :n%snn",xmlstr);
 
 xmlread(xmlstr,strele,value);//after Update
 printf("The Text of Element after Update is :n%snn",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%sn",(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%sn",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%sn",(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%sn",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();
   }
  }
 }
}

-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1