MSXML 解析用法 实例
2011-06-25 21:10
507 查看
/*
CoInitialize(NULL);
CComPtr<IXMLDOMDocument> spXmldoc;
HRESULT hr = spXmldoc.CoCreateInstance(L"MSXML2.DOMDocument.6.0");
if(SUCCEEDED(hr))
{
VARIANT_BOOL isSuccessFul;
CComVariant varXmlFile(L"Demo.xml");
spXmldoc->put_async(VARIANT_FALSE);
HRESULT hr= spXmldoc->load(varXmlFile, &isSuccessFul);
if(isSuccessFul==VARIANT_TRUE)
{
CComBSTR bstrXml;
CComPtr<IXMLDOMElement> spRoot=NULL;
CComPtr<IXMLDOMElement> spTheBook=NULL;
CComPtr<IXMLDOMElement> spTheElem=NULL;
CComPtr<IXMLDOMNode> spNewNode=NULL;
hr = spXmldoc->get_documentElement(&spRoot);
spRoot->get_xml(&bstrXml);
AfxMessageBox("1, 原始的XML");
char *buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
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("2, 新建一本书完成");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 新建一本书完成 ----
////--- 下面对《哈里波特》做一些修改。 ----
////--- 查询找《哈里波特》----
CComPtr<IXMLDOMNode> spTheNode=NULL;
spRoot->selectSingleNode(L"/books/book[name='哈里波特']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox("3,《哈里波特》的XML");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 此时修改这本书的价格 -----
CComPtr<IXMLDOMNodeList> spNodeList=NULL;
CComPtr<IXMLDOMNode> spListItem=NULL;
spTheBook->get_childNodes(&spNodeList);
spNodeList->get_item(1, &spListItem);
spNodeList.Release();
spListItem->put_text(L"15");
////--- 另外还想加一个属性id,值为B01 ----
CComVariant varId(L"B01");
spTheBook->setAttribute(L"id", varId);
varId.Clear();
spTheBook->get_xml(&bstrXml);
spTheBook.Release();
AfxMessageBox("4, 对《哈里波特》修改完成。");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 对《哈里波特》修改完成。 ----
////--- 要用id属性删除《三国演义》这本书 ----
spRoot->selectSingleNode(L"/books/book[@id='B02']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox("5, 《三国演义》的XML");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
CComPtr<IXMLDOMNode> spParentNode=NULL;
spTheBook->get_parentNode(&spParentNode);
spParentNode->removeChild(spTheBook, &spTheNode);
spTheNode.Release();
spParentNode.Release();
spTheBook.Release();
spRoot->get_xml(&bstrXml);
AfxMessageBox("6, 删除《三国演义》后的XML");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 再将所有价格低于10的书删除 ----
spRoot->selectNodes(L"/books/book[price<10]", &spNodeList);
CComQIPtr<IXMLDOMSelection> spSomeBooks=spNodeList;
spNodeList.Release();
spSomeBooks->removeAll();
spSomeBooks.Release();
spXmldoc->get_xml(&bstrXml);
AfxMessageBox("7, 已经删除价格低于10的书");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
spRoot.Release();
bstrXml.Empty();
//spXmldoc->save(varXmlFile); //保存xml。
}
varXmlFile.ClearToZero();
}
spXmldoc.Release();
CoUninitialize();
*/
CoInitialize(NULL);
CComPtr<IXMLDOMDocument> spXmldoc;
HRESULT hr = spXmldoc.CoCreateInstance(L"MSXML2.DOMDocument.6.0");
if(SUCCEEDED(hr))
{
VARIANT_BOOL isSuccessFul;
CComVariant varXmlFile(L"Demo.xml");
spXmldoc->put_async(VARIANT_FALSE);
HRESULT hr= spXmldoc->load(varXmlFile, &isSuccessFul);
if(isSuccessFul==VARIANT_TRUE)
{
CComBSTR bstrXml;
CComPtr<IXMLDOMElement> spRoot=NULL;
CComPtr<IXMLDOMElement> spTheBook=NULL;
CComPtr<IXMLDOMElement> spTheElem=NULL;
CComPtr<IXMLDOMNode> spNewNode=NULL;
hr = spXmldoc->get_documentElement(&spRoot);
spRoot->get_xml(&bstrXml);
AfxMessageBox("1, 原始的XML");
char *buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
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("2, 新建一本书完成");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 新建一本书完成 ----
////--- 下面对《哈里波特》做一些修改。 ----
////--- 查询找《哈里波特》----
CComPtr<IXMLDOMNode> spTheNode=NULL;
spRoot->selectSingleNode(L"/books/book[name='哈里波特']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox("3,《哈里波特》的XML");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 此时修改这本书的价格 -----
CComPtr<IXMLDOMNodeList> spNodeList=NULL;
CComPtr<IXMLDOMNode> spListItem=NULL;
spTheBook->get_childNodes(&spNodeList);
spNodeList->get_item(1, &spListItem);
spNodeList.Release();
spListItem->put_text(L"15");
////--- 另外还想加一个属性id,值为B01 ----
CComVariant varId(L"B01");
spTheBook->setAttribute(L"id", varId);
varId.Clear();
spTheBook->get_xml(&bstrXml);
spTheBook.Release();
AfxMessageBox("4, 对《哈里波特》修改完成。");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 对《哈里波特》修改完成。 ----
////--- 要用id属性删除《三国演义》这本书 ----
spRoot->selectSingleNode(L"/books/book[@id='B02']", &spTheNode);
hr=spTheNode.QueryInterface(&spTheBook);
spTheNode.Release();
spTheBook->get_xml(&bstrXml);
AfxMessageBox("5, 《三国演义》的XML");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
CComPtr<IXMLDOMNode> spParentNode=NULL;
spTheBook->get_parentNode(&spParentNode);
spParentNode->removeChild(spTheBook, &spTheNode);
spTheNode.Release();
spParentNode.Release();
spTheBook.Release();
spRoot->get_xml(&bstrXml);
AfxMessageBox("6, 删除《三国演义》后的XML");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
////--- 再将所有价格低于10的书删除 ----
spRoot->selectNodes(L"/books/book[price<10]", &spNodeList);
CComQIPtr<IXMLDOMSelection> spSomeBooks=spNodeList;
spNodeList.Release();
spSomeBooks->removeAll();
spSomeBooks.Release();
spXmldoc->get_xml(&bstrXml);
AfxMessageBox("7, 已经删除价格低于10的书");
buf = _com_util::ConvertBSTRToString(bstrXml.m_str);
AfxMessageBox(buf);
spRoot.Release();
bstrXml.Empty();
//spXmldoc->save(varXmlFile); //保存xml。
}
varXmlFile.ClearToZero();
}
spXmldoc.Release();
CoUninitialize();
*/
相关文章推荐
- 【Java学习笔记之二十二】解析接口在Java继承中的用法及实例分析
- Android入门之Gallery+ImageSwitcher用法实例解析
- Java的clone()用法实例解析
- [转摘CSDN_cnkiminzhuhu(ID)]详尽解析window.event对象及用法实例
- Python元字符的用法实例解析
- 实例解析Android系统中的ContentProvider组件用法
- Vue.js自定义指令的用法与实例解析
- python命令行参数解析OptionParser类用法实例
- 从LINQ实例解析LINQ的另类用法,解决多条件组合问题
- Android提高之Service用法实例解析
- C#属性(Attribute)用法实例解析
- java之super关键字用法实例解析
- Android实战简易教程<三十>(实例解析Application的用法)
- javascript作用域链(Scope Chain)用法实例解析
- Oracle job的用法的实例解析
- 实例解析jQuery中proxy()函数的用法
- c#中switch case的用法实例解析
- C++中vector的用法实例解析
- C++中vector的用法实例解析