使用VC6.0访问XML文件!

来源:互联网 发布:自己设计室内装修软件 编辑:程序博客网 时间:2024/05/04 19:38

新建了一个类,所有的访问代码都包含在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);
 //}

}

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