热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

XML字符串的读写把持函数及示例程序

以下为引用的内容://OpXML.cpp:Definestheentrypointfortheconsoleapplication.////-----------------------CodedByRonk--------------------------////-------

 

以下为引用的内容:

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

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

#include "stdafx.h"
#include

//---You must Setup the MSXML4.0 before using
#import
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]="LZDioio";
 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 "" 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();
   }
  }
 }
}




推荐阅读
author-avatar
wuyanting67412
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有