API 使用 XML的COM组件

来源:互联网 发布:数据库系统书籍 编辑:程序博客网 时间:2024/04/30 22:10


#include <windows.h>

#import "C:/WINDOWS/system32/msxml3.dll" named_guids
using namespace MSXML2;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  // TODO: Place code here.
 ::CoInitialize(NULL);


 IXMLDOMDocumentPtr m_plDomDocument;
 IXMLDOMElementPtr m_pDocRoot;
 HRESULT hr = m_plDomDocument.CreateInstance(CLSID_DOMDocument);
 if (FAILED(hr))
 {
  _com_error er(hr);
  //AfxMessageBox(er.ErrorMessage());
  //EndDialog(1);
 }

 _bstr_t bstrFileName = "a.xml";
 variant_t vResult;
 vResult = m_plDomDocument->load(bstrFileName);
 if (((bool)vResult) == TRUE) // success!
 {
  // now that the document is loaded, we need to initialize the root pointer
  m_pDocRoot = m_plDomDocument->documentElement;
  //AfxMessageBox("Document loaded successfully!");
 }
 else
 {
  //AfxMessageBox("Document FAILED to load!");
 }

 IXMLDOMNodePtr m_pNode;
 m_pNode = m_pDocRoot->firstChild;
 
 _bstr_t ttt = m_pDocRoot->xml;


 return 0;
}

 

 建个a.xml,内容如下,注意路径

<?xml version="1.0"?>
<autos>
  <manufacturer name="Chevrolet">
    <make name="Corvette">
      <model>2000 Convertible</model>
      <price currency="usd">60,000</price>
      <horsePower>420</horsePower>
      <fuelCapacity units="gallons">18.5</fuelCapacity>
    </make>
  </manufacturer>
  <manufacturer name="Mazda">
    <make name="RX-7">
      <model>test model</model>
      <price currency="usd">30,000</price>
      <horsePower>350</horsePower>
      <fuelCapacity units="gallons">15.5</fuelCapacity>
    </make>
  </manufacturer>
</autos>

 

运行程序,哈哈,最后ttt里面的内容,断到了吧.