欢迎来到天天文库
浏览记录
ID:31610582
大小:32.16 KB
页数:4页
时间:2019-01-15
《使用msxml存取xml(2)》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、使用MsXML存取XML(2)与JavaScript相比而言,在C++中调用COM比较麻烦,MsXML库是一组COM动态库,这意味VisualC++应用程序必须以COM调用方式调用MsXML组件。下面是实现存取persons.xml文件的VisualC++版本。需要导入“msxml4.dll”组件库【程序6-8】使用MsXML读写XML文件1.01#include"stdafx.h"2.02#include"comutil.h"3.03#include4.045.05#import"msxml4.dll"6.
2、067.07voidWritePerson(MSXML2::IXMLDOMDocument2PtrpDocument,8.MSXML2::IXMLDOMNodePtrpParentNode,CStringname,longage)9.08{10.09//创建一个节点11.10MSXML2::IXMLDOMElementPtrpPersonNode=pDocument->createElement(_T("person"));12.11pParentNode->appendChild(pPersonNode);1
3、3.1214.13//写入name15.14MSXML2::IXMLDOMElementPtrpPersonNameNode=pDocument->createElement(_T("name"));16.15pPersonNameNode->text=_bstr_t(name);17.1618.17//写入age19.18MSXML2::IXMLDOMElementPtrpPersonAgeNode=pDocument->createElement(_T("age"));20.19pPersonAgeNode->text=_
4、bstr_t(age);21.2022.21pPersonNode->appendChild(pPersonNameNode);23.22pPersonNode->appendChild(pPersonAgeNode);1.23}2.243.25voidWritePersons()4.26{5.27MSXML2::IXMLDOMDocument2PtrpXMLDocument;6.28pXMLDocument.CreateInstance(__uuidof(MSXML2::DOMDocument));7.29MSXML2::I
5、XMLDOMElementPtrpPersonsNode=8.pXMLDocument->createElement(_T("persons"));9.30pXMLDocument->appendChild(pPersonsNode);10.3111.32WritePerson(pXMLDocument,pPersonsNode,_T("佟湘玉"),28);12.33WritePerson(pXMLDocument,pPersonsNode,_T("白展堂"),27);13.3414.35pXMLDocument->save(
6、_T("persons.xml"));15.36}16.3717.38voidReadPersons()18.39{19.40MSXML2::IXMLDOMDocument2PtrpXMLDocument;20.41pXMLDocument.CreateInstance(__uuidof(MSXML2::DOMDocument));21.4222.43pXMLDocument->load(_T("persons.xml"));23.4424.45MSXML2::IXMLDOMElementPtrpRootNode=pXMLDo
7、cument->documentElement;25.46//查找节点26.47MSXML2::IXMLDOMNodeListPtrpPersonNodes=pRootNode->selectNodes(_T("person"));27.4828.49CStrings;29.50longsize=pPersonNodes->length;30.5131.52for(inti=0;i8、s->item[i];34.551.56MSXML2::IXMLDOMNodePtrpNameNode=pPersonNode->selectSingleNode(_T("name"));2.57MSXML2::IXMLDOMNodePtrpAgeNode=pPersonNo
8、s->item[i];34.551.56MSXML2::IXMLDOMNodePtrpNameNode=pPersonNode->selectSingleNode(_T("name"));2.57MSXML2::IXMLDOMNodePtrpAgeNode=pPersonNo
此文档下载收益归作者所有