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