-- 作者:wFirebird
-- 发布时间:1/17/2006 10:12:00 AM
--
// MSXMLabelDlg.cpp : implementation file // #include "stdafx.h" #include "MSXMLabel.h" #include "MSXMLabelDlg.h" #include "tchar.h" #include "stdio.h" #import "msxml4.dll" using namespace MSXML2; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMSXMLabelDlg dialog CMSXMLabelDlg::CMSXMLabelDlg(CWnd* pParent /*=NULL*/) : CDialog(CMSXMLabelDlg::IDD, pParent) { //{{AFX_DATA_INIT(CMSXMLabelDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMSXMLabelDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMSXMLabelDlg) DDX_Control(pDX, IDC_LISTCTRL, m_listCtrl); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMSXMLabelDlg, CDialog) //{{AFX_MSG_MAP(CMSXMLabelDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDOK, OnLoad) ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMSXMLabelDlg message handlers BOOL CMSXMLabelDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_listCtrl.SetExtendedStyle(LVS_EX_GRIDLINES); m_listCtrl.SetBkColor(RGB(255,255,255)); m_listCtrl.SetTextColor(RGB(0,0,0)); m_listCtrl.SetTextBkColor(RGB(255,255,255)); m_listCtrl.InsertColumn(0,"标签名",LVCFMT_LEFT,100); m_listCtrl.InsertColumn(1,"参数名",LVCFMT_LEFT,100); m_listCtrl.InsertColumn(2,"参数值",LVCFMT_LEFT,100); m_listCtrl.InsertColumn(3,"数据项",LVCFMT_LEFT,200); return TRUE; // return TRUE unless you set the focus to a control } void CMSXMLabelDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CMSXMLabelDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CMSXMLabelDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CMSXMLabelDlg::OnLoad() { CString pathName; CFileDialog OpenFile(TRUE); if (IDOK != OpenFile.DoModal()) { MessageBox("请选择XML文档!"); return; } pathName = OpenFile.GetPathName(); HRESULT hr; MSXML2::IXMLDOMDocumentPtr pDoc = NULL; hr = CoInitialize(NULL); if (FAILED(hr)) { AfxMessageBox("Initialize Failure"); return; } hr = pDoc.CreateInstance("Msxml2.DOMDocument.4.0"); if (FAILED(hr)) { MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!"); return ; } _variant_t varXml(pathName); _variant_t varOut((bool)TRUE); varOut = pDoc->load(varXml); if ((bool)varOut == FALSE) { // Show error description - IXMLDOMParseError sample. MSXML2::IXMLDOMParseErrorPtr errPtr = pDoc->GetparseError(); // Print error details. CString string; string.Format("%L",errPtr->errorCode); MessageBox(string.GetBuffer(NULL),"出错代码"); return ; } MSXML2::IXMLDOMElementPtr pElement = NULL; pElement = pDoc->documentElement; if(pElement == NULL) { AfxMessageBox("文档解析失败!"); return; } BSTR str; pElement->get_tagName(&str); CString cStr(str); m_listCtrl.InsertItem(0,cStr); // str = pElement->Gettext(); // cStr = str; // m_listCtrl.SetItemText(0,3,cStr); MSXML2::IXMLDOMNamedNodeMapPtr pAttributeMap = NULL; MSXML2::IXMLDOMNodePtr pAttributeNode = NULL; VARIANT varValue; pElement->get_attributes(&pAttributeMap); long num = pAttributeMap->length; for (int i = 0; i < num; i++) { pAttributeMap->get_item(i, &pAttributeNode); if (pAttributeNode) { str = pAttributeNode->nodeName; cStr = str; m_listCtrl.SetItemText(0,i + 1,cStr); pAttributeNode->get_nodeValue(&varValue); char strin[30]; strcpy(strin, _bstr_t(varValue) ); cStr = strin; m_listCtrl.SetItemText(0,i + 2,cStr); } } int k = 1; MSXML2::IXMLDOMNodeListPtr pNodeList = NULL; pElement->get_childNodes(&pNodeList); Parse(pNodeList,k); MessageBox(pElement->xml, _T("Document from its root"), MB_OK); } void CMSXMLabelDlg::Parse(MSXML2::IXMLDOMNodeListPtr pNodeList,int &k) { CString cStr; BSTR str; BSTR nodeName; MSXML2::IXMLDOMNodePtr pNode = NULL; long num = pNodeList->length; for(int i = 0; i < num; i++) { pNodeList->get_item(i,&pNode); if (pNode) { MSXML2::IXMLDOMNamedNodeMapPtr pAttributeMap = NULL; MSXML2::IXMLDOMNodePtr pAttributeNode = NULL; VARIANT varValue; pNode->get_nodeName(&nodeName); cStr = nodeName; m_listCtrl.InsertItem(k,cStr); pNode->get_attributes(&pAttributeMap); long attribLength = 0; if (NULL != pAttributeMap) { attribLength = pAttributeMap->length; } if (attribLength) { long attribLength = pAttributeMap->length; for (int j = 0; j < 1; j++) { pAttributeMap->get_item(j ,&pAttributeNode); str = pAttributeNode->nodeName; cStr = str; m_listCtrl.SetItemText(k, j + 1, cStr); pAttributeNode->get_nodeValue(&varValue); char strin[30]; strcpy(strin, _bstr_t(varValue)); cStr = strin; m_listCtrl.SetItemText(k, j + 2, cStr); } } // MessageBox(cStr); // cStr.Format("%d",k); // MessageBox(cStr); k++; MSXML2::IXMLDOMNodeListPtr pNodeChild = NULL; if (SUCCEEDED(pNode->get_childNodes(&pNodeChild))) { Parse(pNodeChild,k); } } } }
|