Read-write operation functions and sample programs for XML strings

Source: Internet
Author: User
Tags functions include printf version
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 ();
}
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.