www.pudn.com > XMLTest.rar > XMLTestDlg.cpp
// XMLTestDlg.cpp : implementation file // #include "stdafx.h" #include "XMLTest.h" #include "XMLTestDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include#import ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CXMLTestDlg dialog CXMLTestDlg::CXMLTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CXMLTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CXMLTestDlg) m_strAuthor = _T(""); m_strID = _T(""); m_strTitle = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CXMLTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CXMLTestDlg) DDX_Text(pDX, IDC_AUTHOR, m_strAuthor); DDX_Text(pDX, IDC_ID, m_strID); DDX_Text(pDX, IDC_TITLE, m_strTitle); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CXMLTestDlg, CDialog) //{{AFX_MSG_MAP(CXMLTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_GENERATE, OnGenerate) ON_BN_CLICKED(IDC_READ, OnRead) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CXMLTestDlg message handlers BOOL CXMLTestDlg::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 ::CoInitialize(NULL); return TRUE; // return TRUE unless you set the focus to a control } void CXMLTestDlg::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 CXMLTestDlg::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 CXMLTestDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CXMLTestDlg::OnGenerate() { // TODO: Add your control notification handler code here UpdateData(); MSXML2::IXMLDOMDocumentPtr pDoc; MSXML2::IXMLDOMElementPtr xmlRoot ; //创建DOMDocument对象 HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40)); if(!SUCCEEDED(hr)) { MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!"); return ; } //根节点的名称为Book //创建元素并添加到文档中 xmlRoot=pDoc->createElement((_bstr_t)"Book"); //设置属性 xmlRoot->setAttribute("id",(const char *)m_strID); pDoc->appendChild(xmlRoot); MSXML2::IXMLDOMElementPtr pNode; //添加“author”元素 pNode=pDoc->createElement((_bstr_t)"Author"); pNode->Puttext((_bstr_t)(const char *)m_strAuthor); xmlRoot->appendChild(pNode); //添加“Title”元素 pNode=pDoc->createElement("Title"); pNode->Puttext((const char *)m_strTitle); xmlRoot->appendChild(pNode); //保存到文件 //如果不存在就建立,存在就覆盖 pDoc->save(".\\Test.XML"); } void CXMLTestDlg::OnRead() { // TODO: Add your control notification handler code here MSXML2::IXMLDOMDocumentPtr pDoc; HRESULT hr; hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40)); if(FAILED(hr)) { MessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!"); return ; } //加载文件 pDoc->load(".\\Test.XML"); MSXML2::IXMLDOMNodePtr temp, tempChild, stateNode; MSXML2::IXMLDOMNodeListPtr tempChildList; MSXML2::IXMLDOMNodeListPtr pNodeList; MSXML2::IXMLDOMNodePtr resultNode; resultNode = pDoc->selectSingleNode("//Book"); MSXML2::IXMLDOMNamedNodeMapPtr attributeMap = resultNode->attributes; MSXML2::IXMLDOMNodePtr listItem; attributeMap->get_item(0, &listItem); m_strID = (char*)(_bstr_t)listItem->nodeTypedValue; pNodeList = pDoc->selectNodes("//Book"); long countOfNode = pNodeList->length; bool flag = false; for(int i = 0; i < countOfNode; i++) { pNodeList->get_item(i, &temp); tempChildList = temp->childNodes; long nCount = tempChildList->length; if (nCount == 2) { tempChildList->get_item(0, &tempChild); m_strAuthor = (char*)(_bstr_t)tempChild->nodeTypedValue; tempChildList->get_item(1, &tempChild); m_strTitle = (char*)(_bstr_t)tempChild->nodeTypedValue; } else { MessageBox(""); } } UpdateData(FALSE); }