xml| Program | functions | example | string
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 the 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 ();
}
}
}
}