关闭

使用VC6.0访问XML文件!

728人阅读 评论(0) 收藏 举报

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

}

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3841次
    • 积分:72
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条