写本文的目的是为了方便大家了解C++ MSXML操作方法。
当然,C++中对MSXML的调用有多种,本文采用的方法是完全参照MSXML SDK提供的文档进行操作。
如果有什么错误,欢迎指正。
代码框架是基于vs2008 MFC 对话框程序(UNICODE)。对话框程序需要读者自己创建。
- #include
- #include
- #pragma comment(lib, "comsuppwd.lib")
- void CXmlSampleDlg::OnBnClickedButton1()
- {
- CoInitialize(NULL);
- CComPtr spXmldoc;
- HRESULT hr = spXmldoc.CoCreateInstance(L"MSXML2.DOMDocument.6.0");
-
- if(SUCCEEDED(hr))
- {
- VARIANT_BOOL isSuccessFul;
- CComVariant varXmlFile(L"a.xml");
-
- spXmldoc->put_async(VARIANT_FALSE);
- HRESULT hr= spXmldoc->load(varXmlFile, &isSuccessFul);
-
- if(isSuccessFul==VARIANT_TRUE)
- {
- CComBSTR bstrXml;
- CComPtr spRoot=NULL;
- CComPtr spTheBook=NULL;
- CComPtr spTheElem=NULL;
- CComPtr spNewNode=NULL;
-
- hr = spXmldoc->get_documentElement(&spRoot);
- spRoot->get_xml(&bstrXml);
- AfxMessageBox(L"1, 原始的XML");
- AfxMessageBox(bstrXml);
-
-
- spXmldoc->createElement(L"book", &spTheBook);
- spXmldoc->createElement(L"name", &spTheElem);
- spXmldoc->put_text(L"新书");
- spTheBook->appendChild(spTheElem, &spNewNode);
- spTheElem.Release();
- spNewNode.Release();
-
- spXmldoc->createElement(L"price", &spTheElem);
- spTheElem->put_text(L"20");
- spTheBook->appendChild(spTheElem, &spNewNode);
- spTheElem.Release();
- spNewNode.Release();
-
- spXmldoc->createElement(L"memo", &spTheElem);
- spTheElem->put_text(L"新书的更好看。");
- spTheBook->appendChild(spTheElem, &spNewNode);
- spNewNode.Release();
- spTheElem.Release();
-
- spRoot->appendChild(spTheBook, &spNewNode);
- spNewNode.Release();
- spTheBook.Release();
-
- spRoot->get_xml(&bstrXml);
- AfxMessageBox(L"2, 新建一本书完成");
- AfxMessageBox(bstrXml);
-
-
-
-
-
- CComPtr spTheNode=NULL;
- spRoot->selectSingleNode(L"/books/book[name='哈里波特']", &spTheNode);
- hr=spTheNode.QueryInterface(&spTheBook);
- spTheNode.Release();
-
- spTheBook->get_xml(&bstrXml);
- AfxMessageBox(L"3,《哈里波特》的XML");
- AfxMessageBox(bstrXml);
-
-
- CComPtr spNodeList=NULL;
- CComPtr spListItem=NULL;
- spTheBook->get_childNodes(&spNodeList);
- spNodeList->get_item(1, &spListItem);
- spNodeList.Release();
- spListItem->put_text(L"15");
-
-
- CComVariant varId(L"B01");
- spTheBook->setAttribute(L"id", varId);
- varId.Clear();
-
- spTheBook->get_xml(&bstrXml);
- spTheBook.Release();
- AfxMessageBox(L"4, 对《哈里波特》修改完成。");
- AfxMessageBox(bstrXml);
-
-
-
-
- spRoot->selectSingleNode(L"/books/book[@id='B02']", &spTheNode);
- hr=spTheNode.QueryInterface(&spTheBook);
- spTheNode.Release();
-
- spTheBook->get_xml(&bstrXml);
- AfxMessageBox(L"5, 《三国演义》的XML");
- AfxMessageBox(bstrXml);
-
- CComPtr spParentNode=NULL;
- spTheBook->get_parentNode(&spParentNode);
- spParentNode->removeChild(spTheBook, &spTheNode);
- spTheNode.Release();
- spParentNode.Release();
- spTheBook.Release();
-
- spRoot->get_xml(&bstrXml);
- AfxMessageBox(L"6, 删除《三国演义》后的XML");
- AfxMessageBox(bstrXml);
-
-
-
- spRoot->selectNodes(L"/books/book[price<10]", &spNodeList);
- CComQIPtr spSomeBooks=spNodeList;
- spNodeList.Release();
-
- spSomeBooks->removeAll();
- spSomeBooks.Release();
-
- spXmldoc->get_xml(&bstrXml);
- AfxMessageBox(L"7, 已经删除价格低于10的书");
- AfxMessageBox(bstrXml);
-
- spRoot.Release();
- bstrXml.Empty();
-
-
- }
- varXmlFile.ClearToZero();
- }
-
- spXmldoc.Release();
-
- CoUninitialize();
转载地址:
http://blog.csdn.net/cds27/article/details/5545455
阅读(4945) | 评论(0) | 转发(1) |