• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

xml 搞代码 7年前 (2018-06-15) 90次浏览 已收录 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[])

http://www.gaodaima.com/34160.htmlXML字符串的读写操作函数及示例程序_xml


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

欢迎大家阅读《XML字符串的读写操作函数及示例程序_xml》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:XML字符串的读写操作函数及示例程序_xml

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址