www.pudn.com > MarkUpTest.rar > MarkUpTestDlg.cpp
// MarkUpTestDlg.cpp : implementation file // #include "stdafx.h" #include "MarkUpTest.h" #include "MarkUpTestDlg.h" #include "XmlAccess.h" #include#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() ///////////////////////////////////////////////////////////////////////////// // CMarkUpTestDlg dialog CMarkUpTestDlg::CMarkUpTestDlg(CWnd* pParent /*=NULL*/) : CDialog(CMarkUpTestDlg::IDD, pParent) { //{{AFX_DATA_INIT(CMarkUpTestDlg) // 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 CMarkUpTestDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMarkUpTestDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMarkUpTestDlg, CDialog) //{{AFX_MSG_MAP(CMarkUpTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_TEST1, OnTest1) ON_BN_CLICKED(IDC_TEST2, OnTest2) ON_BN_CLICKED(IDC_TEST3, OnTest3) ON_BN_CLICKED(IDC_TEST4, OnTest4) ON_BN_CLICKED(IDC_TEST5, OnTest5) ON_BN_CLICKED(IDC_TEST6, OnTest6) ON_BN_CLICKED(IDC_TEST7, OnTest7) ON_BN_CLICKED(IDC_TEST8, OnTest8) ON_BN_CLICKED(IDC_TEST9, OnTest9) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_BN_CLICKED(IDC_BUTTON4, OnButton4) ON_BN_CLICKED(IDC_BUTTON5, OnButton5) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMarkUpTestDlg message handlers BOOL CMarkUpTestDlg::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 if(!AfxOleInit())///初始化COM库 { AfxMessageBox("初始化COM失败"); return FALSE; } return TRUE; // return TRUE unless you set the focus to a control } void CMarkUpTestDlg::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 CMarkUpTestDlg::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 CMarkUpTestDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CMarkUpTestDlg::OnTest1() { // TODO: Add your control notification handler code here CMarkup xml; xml.SetDoc("\r\n"); xml.AddElem("UserInfo"); xml.IntoElem(); xml.AddElem("UserID","小李"); xml.AddElem("UserID","lin"); xml.OutOfElem(); xml.Save("UserInfo.xml"); ShellExecute(NULL,"OPEN","UserInfo.xml",NULL,NULL,SW_NORMAL); } void CMarkUpTestDlg::OnTest2() { //----查看所有UserID---- CMarkup xml; xml.Load("UserInfo.xml"); // BOOL bFind = true; xml.ResetMainPos(); while (xml.FindChildElem("UserID")) { xml.IntoElem(); //Into 以后就不用Child CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetTagName(); strData = xml.GetData(); TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData); MessageBox(strTagName); MessageBox(strData); xml.OutOfElem(); } } /**************这样也可以********************** CMarkup xml; xml.Load("UserInfo.xml"); BOOL bFind = true; xml.ResetMainPos(); xml.FindElem(); //UserInfo xml.IntoElem(); while (xml.FindElem("UserID");) {CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetTagName(); strData = xml.GetData(); TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData); } */ void CMarkUpTestDlg::OnTest3() { //--------把UserID为”luo”改为”flypigluo”----------- BOOL bLoadXml = false; CMarkup xml; bLoadXml = xml.Load("UserInfo.xml"); if (bLoadXml) { CString strUserID = _T(""); xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); while (xml.FindElem("UserID")) { strUserID = xml.GetData(); if (strUserID=="luo") { xml.SetData(CString("flypig")+strUserID); xml.Save("UserInfo.xml"); break; } } } ShellExecute(NULL,"OPEN","UserInfo.xml",NULL,NULL,SW_SHOWNORMAL); } void CMarkUpTestDlg::OnTest4() { BOOL bLoadXml = false; CMarkup xml; bLoadXml = xml.Load("UserInfo.xml"); if (bLoadXml) { xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); xml.AddElem("UserID","luoluo"); xml.OutOfElem(); xml.Save("UserInfo.xml"); } ShellExecute(NULL,"OPEN","UserInfo.xml",NULL,NULL,SW_SHOWNORMAL); } void CMarkUpTestDlg::OnTest5() { BOOL bLoadXml = false; CMarkup xml; bLoadXml = xml.Load("UserInfo.xml"); if (bLoadXml) { xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); xml.InsertElem("UserID","AddUserIDHead"); xml.OutOfElem(); xml.Save("UserInfo.xml"); } ShellExecute(NULL,"OPEN","UserInfo.xml",NULL,NULL,SW_SHOWNORMAL); } void CMarkUpTestDlg::OnTest6() { CMarkup xml; xml.Load("UserInfo.xml"); BOOL bFind = true; xml.ResetMainPos(); while (bFind) { bFind = xml.FindChildElem("UserID"); if (bFind) { //此时接点还是父接点 CString strData = _T(""); strData = xml.GetChildData(); if (strData=="AddUserIDHead") { xml.RemoveChildElem(); xml.Save("UserInfo.xml"); break; } } } ShellExecute(NULL,"OPEN","UserInfo.xml",NULL,NULL,SW_SHOWNORMAL); } void CMarkUpTestDlg::OnTest7() { CString strID = _T(""); CString strPwd = _T(""); CMarkup xml; xml.SetDoc(""); xml.AddElem("UserInfo"); xml.IntoElem(); for (int i=0; i<10; i++) { //产生一个随机数,作为权限 srand(UINT(time(NULL)+i)); int nLevel = rand()%256; strID.Format("UserID%02d",i); strPwd.Format("UserPwd%02d",i); xml.AddElem("User"); xml.AddAttrib("UserID",strID); xml.AddAttrib("UserPwd",strPwd); xml.AddAttrib("UserLevel",nLevel); } xml.OutOfElem(); xml.Save("UserInfo1.xml"); ShellExecute(NULL,"OPEN","UserInfo1.xml",NULL,NULL,SW_SHOWNORMAL); } void CMarkUpTestDlg::OnTest8() { CMarkup xml; BOOL bLoad = false; BOOL bFind = false; bLoad = xml.Load("UserInfo1.xml"); if (bLoad) { CString strID; CString strPwd; xml.ResetMainPos(); xml.FindElem(); //UserInfo while (xml.FindChildElem("User")) { strID = xml.GetChildAttrib("UserID"); strPwd = xml.GetChildAttrib("UserPwd"); TRACE("\n----id:%s,pwd:%s-------\n",strID,strPwd); if (strID=="UserID10"&&strPwd=="UserPwd00") { bFind = true; break; } } } if (bFind) { TRACE("\n---- find------\n"); MessageBox("*******Find*******"); } else { TRACE("\n----no find------\n"); MessageBox("*******no Find*******"); } } void CMarkUpTestDlg::OnTest9() { CMarkup xml; BOOL bLoad = false; BOOL bFind = false; bLoad = xml.Load("UserInfo1.xml"); if (bLoad) { CString strID; CString strPwd; xml.ResetMainPos(); xml.FindElem(); //UserInfo while (xml.FindChildElem("User")) { strID = xml.GetChildAttrib("UserID"); strPwd = xml.GetChildAttrib("UserPwd"); TRACE("\n----id:%s,pwd:%s-------\n",strID,strPwd); if (strID=="UserID00"&&strPwd=="UserPwd00") { bFind = true; xml.SetChildAttrib("UserID",strID+CString("Modify")); xml.Save("UserInfo1.xml"); break; } } } if (bFind) { TRACE("\n---- find------\n"); MessageBox("*****Find*****"); } else { TRACE("\n----no find------\n"); MessageBox("*****No Find*****"); } ShellExecute(NULL,"OPEN","UserInfo1.xml",NULL,NULL,SW_SHOWNORMAL); } void CMarkUpTestDlg::OnButton1() { CMarkup xml; BOOL bLoad = false; BOOL bFind = false; bLoad = xml.Load("UserInfo.xml"); // xml.ResetMainPos(); xml.FindElem(); //UserInfo xml.IntoElem(); while (xml.FindElem("CODE")) { CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetTagName(); strData = xml.GetData(); MessageBox(strData); TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData); } } void CMarkUpTestDlg::OnButton2() { CXmlAccess xmlAccess; static char szTemp[]= " "; CComBSTR capitalAccountsInfoXML = szTemp;// _bstr_t outputXml(capitalAccountsInfoXML, TRUE); // outputXml = " "; xmlAccess.LoadXML(outputXml); _variant_t vt; _variant_t Vt = "UserInfo.xml"; xmlAccess.LoadXML(Vt); // 取得根结点 xmlAccess.m_RootElementPtr=xmlAccess.pXMLDoc->GetdocumentElement(); // 取得此结点下的记录集 xmlAccess.m_RecordSetPtr=xmlAccess.m_RootElementPtr->GetchildNodes(); // HRESULT hresult; // IDispatch FAR* pdisp = (IDispatch FAR*)NULL; // DISPID dispid; // OLECHAR FAR* szMember = "color"; // xmlAccess.m_RecordSetPtr->GetIDsOfNames(IID_NULL,&szMember,1,LOCALE_SYSTEM_DEFAULT,&dispid); xmlAccess.m_RecordPtr=xmlAccess.m_RecordSetPtr->Getitem(0); IXMLDOMNodePtr pNode; // IXMLDOMNode node; // HRESULT hrResult; CString strTmp; pNode = xmlAccess.m_RecordPtr->GetfirstChild(); pNode = xmlAccess.m_RecordPtr; while (pNode) { CString strNodeName; strNodeName = (LPCTSTR)(_bstr_t)pNode->GetnodeName(); MessageBox(strNodeName); strTmp = (LPCTSTR)(_bstr_t)pNode->Gettext(); MessageBox(strTmp); // hrResult = pNode->selectSingleNode("UPGRADE"); // // if(hrResult == S_OK) // { // strTmp = (LPCTSTR)(_bstr_t)pNode->Gettext(); // MessageBox(strTmp); // } pNode = pNode->GetnextSibling(); } } CString GetFieldInfo(IXMLDOMNodePtr pRecordPtr , CString nodeName) { CString strNodeInfo(""); CString strNodeName(""); IXMLDOMNodePtr pNode; pNode = pRecordPtr->GetfirstChild(); while(pNode) { strNodeName = (LPCTSTR)(_bstr_t)pNode->GetnodeName(); if (!strcmp(strNodeName , nodeName)) { strNodeInfo = (LPCTSTR)(_bstr_t)pNode->Gettext(); break; } pNode = pNode->GetnextSibling(); } return strNodeInfo; } IXMLDOMNodePtr GetChildField(IXMLDOMNodePtr pRecordPtr , CString nodeName) { CString strNodeName(""); IXMLDOMNodePtr pNode; pNode = pRecordPtr->GetfirstChild(); while(pNode) { strNodeName = (LPCTSTR)(_bstr_t)pNode->GetnodeName(); if (!strcmp(strNodeName , nodeName)) { break; } pNode = pNode->GetnextSibling(); } return pNode; } void CMarkUpTestDlg::OnButton3() { CXmlAccess xmlAccess; _variant_t vt; _variant_t Vt = "UserInfo.xml"; xmlAccess.LoadXML(Vt); // 取得根结点 xmlAccess.m_RootElementPtr=xmlAccess.pXMLDoc->GetdocumentElement(); // 取得此结点下的记录集 xmlAccess.m_RecordSetPtr=xmlAccess.m_RootElementPtr->GetchildNodes(); xmlAccess.m_RecordPtr=xmlAccess.m_RecordSetPtr->Getitem(2); IXMLDOMNodePtr pNode,pNode2; CString strTmp; pNode = xmlAccess.m_RecordPtr->GetfirstChild(); // pNode = xmlAccess.m_RecordPtr; while (pNode) { CString strNodeName; strNodeName = (LPCTSTR)(_bstr_t)pNode->GetnodeName(); if (!strcmp(strNodeName,"ACCOUNT")) { pNode = pNode->GetfirstChild(); while (pNode) { strNodeName = (LPCTSTR)(_bstr_t)pNode->GetnodeName(); MessageBox(strNodeName); strTmp = (LPCTSTR)(_bstr_t)pNode->Gettext(); // IXMLDOMNamedNodeMapPtr x = pNode->attributes; // pNode2 = pNode->attributes->Getitem(0); pNode2 = pNode->attributes->getNamedItem("PRIMARY"); if (pNode2 !=NULL) { strTmp = (LPCTSTR)pNode2->Gettext(); if (strTmp == "TRUE") { MessageBox(strTmp); } } pNode = pNode->GetnextSibling(); } } else pNode = pNode->GetnextSibling(); } } void CMarkUpTestDlg::OnButton4() { CXmlAccess xmlAccess; _variant_t vt; _variant_t Vt = "UserInfo.xml"; xmlAccess.LoadXML(Vt); // 取得根结点 xmlAccess.m_RootElementPtr=xmlAccess.pXMLDoc->GetdocumentElement(); // 取得此结点下的记录集 xmlAccess.m_RecordSetPtr=xmlAccess.m_RootElementPtr->GetchildNodes(); xmlAccess.m_RecordPtr=xmlAccess.m_RecordSetPtr->Getitem(2); CString strTmp; strTmp = GetFieldInfo(xmlAccess.m_RecordPtr,"CUST_ID"); MessageBox(strTmp); IXMLDOMNodePtr pNode,pNodeTemp; pNode = GetChildField(xmlAccess.m_RecordPtr,"ACCOUNT"); pNodeTemp = pNode->GetfirstChild(); while (pNodeTemp) { strTmp = (LPCTSTR)(_bstr_t)pNodeTemp->Gettext(); MessageBox(strTmp); IXMLDOMNodePtr pNode2 = pNodeTemp->attributes->getNamedItem("PRIMARY"); if (pNode2) { strTmp = (LPCSTR)pNode2->Gettext(); MessageBox(strTmp); } pNodeTemp = pNodeTemp->GetnextSibling(); } // strTmp = GetFieldInfo(pNode,"ACCOUNT_NO"); // MessageBox(strTmp); } void CMarkUpTestDlg::OnButton5() { CXmlAccess xmlAccess; _variant_t vt; _variant_t Vt = "ORDER_STATUS.xml"; xmlAccess.LoadXML(Vt); // 取得根结点 xmlAccess.m_RootElementPtr=xmlAccess.pXMLDoc->GetdocumentElement(); // 取得此结点下的记录集 xmlAccess.m_RecordSetPtr=xmlAccess.m_RootElementPtr->GetchildNodes(); xmlAccess.m_RecordPtr=xmlAccess.m_RecordSetPtr->Getitem(1); CString strTmp; strTmp = GetFieldInfo(xmlAccess.m_RecordPtr,"NAME"); MessageBox(strTmp); strTmp = (LPCTSTR)xmlAccess.m_RecordPtr->attributes->getNamedItem("ALLOW_CANCEL")->Gettext(); MessageBox(strTmp); }