使用VC6.0访问XML文件!

新建了一个类,所有的访问代码都包含在MyTest类中了!

(四年以前使用过VC,这是四年以后第一个VC程序)

//=========================================================================

头文件:MyTest.h的代码如下  

//导入操作DOM的COM
#import <msxml.dll> named_guids
using namespace MSXML;
//....................................................
class MyTest {
         public:
                   MyTest();
                   virtual ~MyTest();
                   void testme();
                   //定义操作DOM对象的指针
                  IXMLDOMDocumentPtr m_plDomDocument;
                  IXMLDOMElementPtr m_pDocRoot;
                  void DisplayChild(IXMLDOMNodePtr pChild);
                  void DisplayChildren(IXMLDOMNodePtr pParent);
};

//============================================================================

实现文件:MyTest.cpp

MyTest::MyTest(){
                   //可能是开始使用COM
                   ::AfxOleInit();
                   ::CoInitialize(NULL);
                   HRESULT hr = m_plDomDocument.CreateInstance(CLSID_DOMDocument);
                   if (FAILED(hr)){
                       _com_error er(hr);
                      AfxMessageBox(er.ErrorMessage());
                     //EndDialog(1);
                  }
}

MyTest::~MyTest(){

}
void MyTest::testme(){
                   //开始操作XML文档了
                   CString strFileName ("my.xml");
                   // convert xml file name string to something COM can handle (BSTR)
                   _bstr_t bstrFileName;
                  bstrFileName = strFileName.AllocSysString();

                   // call the IXMLDOMDocumentPtr's load function to load the XML document
                  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!");
                  }
                AfxMessageBox(m_plDomDocument->xml);
}

void MyTest::DisplayChildren(IXMLDOMNodePtr pParent){
                    // display the current node's name
                   DisplayChild(pParent);

                   // simple for loop to get all children
                   for (IXMLDOMNodePtr pChild = pParent->firstChild;
                        NULL != pChild;
                        pChild = pChild->nextSibling){
                    // for each child, call this function so that we get
                    // its children as well
                    DisplayChildren(pChild);
 }
}

void MyTest::DisplayChild(IXMLDOMNodePtr pChild){
 //if (class="codeHighlight">NODE_TEXT == pChild->nodeType)
 //{
  //AfxMessageBox(pChild->text);
 //}
 //else
 //{
  //AfxMessageBox(pChild->nodeName);
 //}

}

//=================================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值