使用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#include<clocale>
4.04
5.05#import"msxml4.dll"
6.06
7.07voidWritePerson(MSXML2::IXMLDOMDocument2PtrpDocument,
8.MSXML2::IXMLDOMNodePtrpParentNode,CStringname,longage)
9.08{
10.09//创建一个<person>节点
11.10MSXML2::IXMLDOMElementPtrpPersonNode=
pDocument->createElement(_T("person"));
12.11pParentNode->appendChild(pPersonNode);
13.12
14.13//写入name
15.14MSXML2::IXMLDOMElementPtrpPersonNameNode=
pDocument->createElement(_T("name"));
16.15pPersonNameNode->text=_bstr_t(name);
17.16
18.17//写入age
19.18MSXML2::IXMLDOMElementPtrpPersonAgeNode=
pDocument->createElement(_T("age"));
20.19pPersonAgeNode->text=_bstr_t(age);
21.20
22.21pPersonNode->appendChild(pPersonNameNode);
23.22pPersonNode->appendChild(pPersonAgeNode);
24.23}
25.24
26.25voidWritePersons()
27.26{
28.27MSXML2::IXMLDOMDocument2PtrpXMLDocument;
29.28pXMLDocument.CreateInstance(__uuidof(MSXML2::DOMDocument));
30.29MSXML2::IXMLDOMElementPtrpPersonsNode=
31.pXMLDocument->createElement(_T("persons"));
32.30pXMLDocument->appendChild(pPersonsNode);
33.31
34.32WritePerson(pXMLDocument,pPersonsNode,_T("佟湘玉"),28);
35.33WritePerson(pXMLDocument,255)">"白展堂"),27);
36.34
37.35pXMLDocument->save(_T("persons.xml"));
38.36}
39.37
40.38voidReadPersons()
41.39{
42.40MSXML2::IXMLDOMDocument2PtrpXMLDocument;
43.41pXMLDocument.CreateInstance(__uuidof(MSXML2::DOMDocument));
44.42
45.43pXMLDocument->load(_T("persons.xml"));
46.44
47.45MSXML2::IXMLDOMElementPtrpRootNode=
pXMLDocument->documentElement;
48.46//查找<person>节点
49.47MSXML2::IXMLDOMNodeListPtrpPersonNodes=
pRootNode->selectNodes(_T("person"));
50.48
51.49CStrings;
52.50longsize=pPersonNodes->length;
53.51
54.52for(inti=0;i<size;i++)
55.53{
56.54MSXML2::IXMLDOMNodePtrpPersonNode=
pPersonNodes->item[i];
57.55
58.56MSXML2::IXMLDOMNodePtrpNameNode=
pPersonNode->selectSingleNode(_T("name"));
59.57MSXML2::IXMLDOMNodePtrpAgeNode=
pPersonNode->selectSingleNode(_T("age"));
60.58
61.59s.Format(_T("%sname:%s\tage:%s\r\n"),
s,(LPCTSTR)pNameNode->
62.text,87)">LPCTSTR)pAgeNode->text);
63.60}
64.61
65.62_tprintf(s);
66.63}
67.64
68.65int_tmain()
69.66{
70.67setlocale(LC_ALL,"chs");
71.68
72.69HRESULThr;
73.70hr=::CoInitialize(NULL);
74.71
75.72WritePersons();
76.73ReadPersons();
77.74
78.75::CoUninitialize();
79.76return0;77}
运行结果如图6-27所示。
图6-27运行结果 |
在VisualC++中,调用COM组件比较省力、安全的办法是使用#import方式。另外,MsXML的版本不同,其提供的DLL文件名也不同,如MSXML4.0对应的DLL为"msxml4.dll"。细心的读者可能会发现,一旦在程序中导入了msxml4.dll并build(生成)一次项目,在程序中就可以自动引入一些类,如:MSXML2::IXMLDOMDocument2Ptr,MSXML2是一个C++名字空间,IXMLDOMDocument2Ptr则是一个封装了IXMLDOMDocument对象操作的智能指针类。好奇的读者可以选择"IXMLDOMDocument2Ptr",并查看其定义,就可以跟踪到Debug目录下一个叫着msxml4.tlh的头文件,它是由VisualC++从msxml4.dll中自动提取并生成的C++头文件,与此一起生成的还有其他的文件,如msxml4.tli等。
原文链接:https://www.f2er.com/xml/299694.html