使用MsXML存取XML(2)
@H_403_14@与JavaScript相比而言,在C++中调用COM比较麻烦,MsXML库是一组COM动态库,这意味VisualC++应用程序必须以COM调用方式调用MsXML组件。 @H_403_14@下面是实现存取persons.xml文件的VisualC++版本。 @H_403_14@需要导入“msxml4.dll”组件库 @H_403_14@【程序6-8】使用MsXML读写XML文件 @H_403_14@1.01#include"stdafx.h" @H_403_14@2.02#include"comutil.h" @H_403_14@3.03#include<clocale> @H_403_14@4.04 @H_403_14@5.05#import"msxml4.dll" @H_403_14@6.06 @H_403_14@7.07voidWritePerson(MSXML2::IXMLDOMDocument2PtrpDocument, @H_403_14@8.MSXML2::IXMLDOMNodePtrpParentNode,CStringname,longage) @H_403_14@9.08{ @H_403_14@10.09//创建一个<person>节点 @H_403_14@11.10MSXML2::IXMLDOMElementPtrpPersonNode=pDocument->createElement(_T("person")); @H_403_14@12.11pParentNode->appendChild(pPersonNode); @H_403_14@13.12 @H_403_14@14.13//写入name @H_403_14@15.14MSXML2::IXMLDOMElementPtrpPersonNameNode=
pDocument->createElement(_T("name")); @H_403_14@16.15pPersonNameNode->text=_bstr_t(name); @H_403_14@17.16 @H_403_14@18.17//写入age @H_403_14@19.18MSXML2::IXMLDOMElementPtrpPersonAgeNode=
pDocument->createElement(_T("age")); @H_403_14@20.19pPersonAgeNode->text=_bstr_t(age); @H_403_14@21.20 @H_403_14@22.21pPersonNode->appendChild(pPersonNameNode); @H_403_14@23.22pPersonNode->appendChild(pPersonAgeNode); @H_403_14@24.23} @H_403_14@25.24 @H_403_14@26.25voidWritePersons() @H_403_14@27.26{ @H_403_14@28.27MSXML2::IXMLDOMDocument2PtrpXMLDocument; @H_403_14@29.28pXMLDocument.CreateInstance(__uuidof(MSXML2::DOMDocument)); @H_403_14@30.29MSXML2::IXMLDOMElementPtrpPersonsNode= @H_403_14@31.pXMLDocument->createElement(_T("persons")); @H_403_14@32.30pXMLDocument->appendChild(pPersonsNode); @H_403_14@33.31 @H_403_14@34.32WritePerson(pXMLDocument,pPersonsNode,_T("佟湘玉"),28); @H_403_14@35.33WritePerson(pXMLDocument,255)">"白展堂"),27); @H_403_14@36.34 @H_403_14@37.35pXMLDocument->save(_T("persons.xml")); @H_403_14@38.36} @H_403_14@39.37 @H_403_14@40.38voidReadPersons() @H_403_14@41.39{ @H_403_14@42.40MSXML2::IXMLDOMDocument2PtrpXMLDocument; @H_403_14@43.41pXMLDocument.CreateInstance(__uuidof(MSXML2::DOMDocument)); @H_403_14@44.42 @H_403_14@45.43pXMLDocument->load(_T("persons.xml")); @H_403_14@46.44 @H_403_14@47.45MSXML2::IXMLDOMElementPtrpRootNode=
pXMLDocument->documentElement; @H_403_14@48.46//查找<person>节点 @H_403_14@49.47MSXML2::IXMLDOMNodeListPtrpPersonNodes=
pRootNode->selectNodes(_T("person")); @H_403_14@50.48 @H_403_14@51.49CStrings; @H_403_14@52.50longsize=pPersonNodes->length; @H_403_14@53.51 @H_403_14@54.52for(inti=0;i<size;i++) @H_403_14@55.53{ @H_403_14@56.54MSXML2::IXMLDOMNodePtrpPersonNode=
pPersonNodes->item[i]; @H_403_14@57.55 @H_403_14@58.56MSXML2::IXMLDOMNodePtrpNameNode=
pPersonNode->selectSingleNode(_T("name")); @H_403_14@59.57MSXML2::IXMLDOMNodePtrpAgeNode=
pPersonNode->selectSingleNode(_T("age")); @H_403_14@60.58 @H_403_14@61.59s.Format(_T("%sname:%s\tage:%s\r\n"),
s,(LPCTSTR)pNameNode-> @H_403_14@62.text,87)">LPCTSTR)pAgeNode->text); @H_403_14@63.60} @H_403_14@64.61 @H_403_14@65.62_tprintf(s); @H_403_14@66.63} @H_403_14@67.64 @H_403_14@68.65int_tmain() @H_403_14@69.66{ @H_403_14@70.67setlocale(LC_ALL,"chs"); @H_403_14@71.68 @H_403_14@72.69HRESULThr; @H_403_14@73.70hr=::CoInitialize(NULL); @H_403_14@74.71 @H_403_14@75.72WritePersons(); @H_403_14@76.73ReadPersons(); @H_403_14@77.74 @H_403_14@78.75::CoUninitialize(); @H_403_14@79.76return0;77} @H_403_14@运行结果如图6-27所示。
@H_403_14@图6-27运行结果 |