⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmltestdlg.cpp

📁 xml解析程序
💻 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

/////////////////////////////////////////////////////////////////////////////
// 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 ction 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)
		// 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 CXmltestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CXmltestDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}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_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButt_find)
	ON_BN_CLICKED(IDC_BUTTON3, OnButt_try)
	ON_BN_CLICKED(IDC_BUTTON4, OnButt_find2)
	ON_BN_CLICKED(IDC_BUTT_FINDID, OnButtFindid)
	//}}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
	
	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;
}

//各层当前父节点位置
NODEPOS node_levelfirst[6];
//待解析的数据流字符
char totalbuf[1024*20];


	//节点数组
	ORGITEMINFO m_OrgListInfo[100];

//节点类结构数组
MyOrg OrgTreeNode[1000];
int iSerino=0;
MyOrg *root;
int tryfun(char *keyvalue,char *bufRet)
{
	char sztotalbuf[1024*50]={0};
		for(int j=0;j<iSerino;j++)
		{
			if(strcmp(keyvalue,OrgTreeNode[j].m_id)==0)
			{
				POSITION pos = OrgTreeNode[j].m_subOrgs.GetHeadPosition();
				int nKey;
				MyOrg *pt;
				sprintf(sztotalbuf,"%s %s sub:",OrgTreeNode[j].m_id,OrgTreeNode[j].m_name);
				while (pos != NULL)
				{
					pt=(MyOrg *)OrgTreeNode[j].m_subOrgs.GetNext(pos);
					strcat(sztotalbuf,pt->m_name);
				}
				pos = OrgTreeNode[j].m_subUsers.GetHeadPosition();
				while (pos != NULL)
				{

					pt=(MyOrg *)OrgTreeNode[j].m_subOrgs.GetNext(pos);
					strcat(sztotalbuf,pt->m_name);
				}
				break;
			}
		}
	strcpy(bufRet,sztotalbuf);

	return 1;
}

//在组织树结构中以名字作为key查找节点

MyOrg *FindOrgByName(MyOrg *pttree,char *strName)
{
	POSITION pos = pttree->m_subOrgs.GetHeadPosition();
	BOOL bfindflag=FALSE;
	MyOrg *pt=NULL,*pt2=NULL;
	if(pttree==NULL)
		return NULL;
	else if(strcmp(strName,pttree->m_name)==0)
		bfindflag=TRUE;
	else
	{
		while (pos != NULL)
		{
			pt=(MyOrg *)pttree->m_subOrgs.GetNext(pos);
			if(strcmp(strName,pt->m_name)==0)
			{	bfindflag=TRUE;
			break;
			}
			else	//return
			{
				MyOrg *pRet=FindOrgByName(pt,strName);
				if(pRet !=NULL)
				{
					bfindflag=TRUE;
					pt=pRet;
					break;
				}
			}
		}	
		if(bfindflag !=TRUE)
		{
			POSITION posemployee =pttree->m_subUsers.GetHeadPosition();
			while (posemployee != NULL)
			{
				pt2=(MyOrg *)pttree->m_subUsers.GetNext(posemployee);
				//分公司:(LPSTR)(LPCTSTR)
				if(strcmp(strName,pt2->m_name)==0)
				{	bfindflag=TRUE;
				pt=pt2;
				break;
				}
			}
		}
	}
	if(bfindflag ==TRUE)
		return pt;
	//pos= NULL
	else
		return NULL;
}

//10.19
////////--------------------------------------------
//在组织树结构中以ID作为key查找节点

MyOrg *FindOrgById(MyOrg *pttree,char *strId)
{
	POSITION pos = pttree->m_subOrgs.GetHeadPosition();
	BOOL bfindflag=FALSE;
	MyOrg *pt=NULL,*pt2 =NULL;
	if(pttree==NULL)
		return NULL;
	else if(strcmp(strId,pttree->m_id)==0)
		bfindflag=TRUE;
	else
	{
		while (pos != NULL)
		{
			pt=(MyOrg *)pttree->m_subOrgs.GetNext(pos);
			//分公司:(LPSTR)(LPCTSTR)
			if(strcmp(strId,pt->m_id)==0)
			{	bfindflag=TRUE;
				break;
			}
			else	//return
			{
				MyOrg *pRet=FindOrgById(pt,strId);
				if(pRet !=NULL)
				{
					bfindflag=TRUE;
					pt=pRet;
					break;
				}
			}
		}
		if(bfindflag !=TRUE)
		{
			POSITION posemployee =pttree->m_subUsers.GetHeadPosition();
			while (posemployee != NULL)
			{
				pt2=(MyOrg *)pttree->m_subUsers.GetNext(posemployee);
				//分公司:(LPSTR)(LPCTSTR)
				if(strcmp(strId,pt2->m_id)==0)
				{	bfindflag=TRUE;
					pt=pt2;
					break;
				}
			}
		}

	}
	if(bfindflag ==TRUE)
		return pt;
	//pos= NULL
	else
		return NULL;
}

BOOL CXmltestDlg::GetXmlOrgData(char *szErrinfo)
{

	char descbuf[100];
	char szErrorText[100];
	int m_iOBUNum=0;
	CString buffersend; 
	CStdioFile cPhoneFile;
	BOOL uiReadFlag;
	char tmpflag;
	char szlocalpath[30],xmllinedata[80];
	bfirstoper=TRUE;
	//节点级别
	nodelevel=0;
	FILE *fp;
	int revlen=0,iVarlen=0;
	memset(szlocalpath,0,sizeof(szlocalpath));
	memset(descbuf,0,sizeof(descbuf));
	memset(totalbuf,0,sizeof(totalbuf));
	strcpy(szlocalpath,".\\org55.xml");
	memset(xmllinedata,0,sizeof(xmllinedata));
		
	
	memset(m_OrgListInfo,0,sizeof(ORGITEMINFO)*100);
	int inum;
	//add
	MyOrg *pCurrent;
	//MyOrg *
	root=&OrgTreeNode[0];
	if((fp=fopen(szlocalpath,"r"))!=NULL)
	{
		int isize=fread(totalbuf,1,1024*20,fp);
		//get level
		char* pstr;
		m_ilevelnum=0;
		pstr=totalbuf;
		pstr=strstr(totalbuf,"node");
		int node_level=0;
		uiReadFlag=getsectiondata(descbuf,&node_level);
		m_iOBUNum=0;
		//读一行字符串
		//root.m_name=...
		MyOrg *node;
		while(uiReadFlag)
		{
			memset((char *)&m_OrgListInfo[m_iOBUNum],0,sizeof(ORGITEMINFO));
			g_GetxmlnewItem(descbuf,m_OrgListInfo[m_iOBUNum].szORGID);
			g_GetxmlnewItem(descbuf,m_OrgListInfo[m_iOBUNum].szNANE);

			char *sTemEnd;
			sTemEnd=(char *)memchr(descbuf,'=',strlen(descbuf));
			char tmpbyte;
			iVarlen=sTemEnd -descbuf;
			//tmpbyte=descbuf[iVarlen+2];
			tmpbyte=*(sTemEnd+2);
			m_OrgListInfo[m_iOBUNum].szORGTYPE=atoi(&tmpbyte);
			//add
			if(m_iOBUNum==0)
			{	
				strcpy(root->m_id,m_OrgListInfo[0].szORGID);
				strcpy(root->m_name,m_OrgListInfo[0].szNANE);
				root->m_nType=m_OrgListInfo[0].szORGTYPE;
				pCurrent= root;
			}
			else
			{
				//当前节点
				node= &OrgTreeNode[iSerino];	//new MyOrg();
				strcpy(node->m_id,m_OrgListInfo[m_iOBUNum].szORGID);
				strcpy(node->m_name,m_OrgListInfo[m_iOBUNum].szNANE);
				node->m_nType=m_OrgListInfo[m_iOBUNum].szORGTYPE;
				
				//pCurrent->AddSub(node);
			}
			if(node_levelfirst[node_level].bOrgFlag==FALSE)
			{
				node_levelfirst[node_level].bOrgFlag=TRUE;
				node_levelfirst[node_level].byNum=node_level;
				strcpy(node_levelfirst[node_level].byNodeId,m_OrgListInfo[m_iOBUNum].szORGID);
				//p数组	m_iOBUNum
				if(node_level==0)
					node_levelfirst[node_level].pCurrent=root;
				else
				{
					node_levelfirst[node_level].pCurrent=node;
				}
			}
			m_OrgListInfo[m_iOBUNum].npParent.byNum=node_level-1;
			strcpy(m_OrgListInfo[m_iOBUNum].npParent.byNodeId,node_levelfirst[node_level-1].byNodeId);
			
			//-------10.18
			if(node_level>=1)
			{
				//父节点
				node_levelfirst[node_level-1].pCurrent->AddSub(&(*node));
				//子节点
				node->pCurrent=node_levelfirst[node_level-1].pCurrent;
			}
			m_iOBUNum++;
			memset(descbuf,0,sizeof(descbuf));
			uiReadFlag=getsectiondata(descbuf,&node_level);
//			CPtrList<
			iSerino++;
		}
	}
	else
	{
		sprintf(szErrorText,"Open %s Error:\n",szlocalpath);
		strcpy(szErrinfo,szErrorText);
		return FALSE;
	}
	
	char sztrybuf[80]={0};
	POSITION pos = root->m_subOrgs.GetHeadPosition();
	int nKey;
	MyOrg *pt;
	while (pos != NULL)
	{
		pt=(MyOrg *)root->m_subOrgs.GetNext(pos);
		strcat(sztrybuf,pt->m_name);

	}
//	POSITION pos2 = node_levelfirst[1].pCurrent->m_subOrgs.GetHeadPosition();
	char sztotalbuf[1024*20]={0};
	
	tryfun("60001",sztotalbuf);	
	MessageBox(sztotalbuf,"取得数据");
	fclose(fp);
	return TRUE;
}

bool CXmltestDlg::getsectiondata(char *sectiondata,int *node_level)
{
	char *sTemKey1,*sTemKey2,*sTemKey0;
	int iVarlinelen=0,iVartotallen,revlen=0;
	int iemptynum=0;
	sTemKey1=strstr(totalbuf,"node");
	iVartotallen=strlen(totalbuf);
	if (iVartotallen<25)
		return FALSE;
	revlen=sTemKey1 -totalbuf;
	sTemKey2=(char *)memchr(sTemKey1+1,'>',iVartotallen);
	iVarlinelen=sTemKey2-totalbuf-revlen;
	//level
	//empty
	if(bfirstoper==TRUE)
		bfirstoper=FALSE;
	else
	{
		sTemKey0=(char *)memchr(totalbuf,' ',iVartotallen);
		iemptynum=sTemKey1-sTemKey0-1;
		m_ilevelnum=iemptynum/2;
	}
	//<\node> <\node> next line
	//if
	int ideclen=0;
	while(iVarlinelen<8)
	{
		ideclen +=iVarlinelen;
		//memcpy(totalbuf,sTemKey2+1,iVartotallen);
		sTemKey1=strstr(sTemKey1+1,"node");
		iVartotallen=strlen(totalbuf);
		//10
		if (iVartotallen<25)
			return FALSE;
		revlen=sTemKey1 -totalbuf;
		sTemKey2=(char *)memchr(sTemKey1+1,'>',iVartotallen);
		iVarlinelen=sTemKey2-totalbuf-revlen;//-ideclen;
		//level
		char *itempos=strstr(totalbuf,"/node");
		sTemKey0=(char *)memchr(itempos+1,' ',iVartotallen);
		iemptynum=sTemKey1-sTemKey0-1;
		m_ilevelnum=iemptynum/2;
		//set	-1
	node_levelfirst[m_ilevelnum].bOrgFlag=FALSE;
		node_levelfirst[m_ilevelnum+1].bOrgFlag=FALSE;
	}
		*node_level=m_ilevelnum;//1;

	iVartotallen -=iVarlinelen;
	//linedata
	memcpy(sectiondata,sTemKey1,iVarlinelen);
	//remine data;
	memcpy(totalbuf,sTemKey2+1,iVartotallen);
	return TRUE;
}
int CXmltestDlg::g_GetxmlnewItem(char *revStr,char *xmlorgInfo)
{
	int ipos=0,revlen,iVarlen=0;
	revlen=strlen(revStr);
	char *sTemEnd1,*sTemEnd2;
	sTemEnd1=(char *)memchr(revStr,'=',revlen);
	ipos=sTemEnd1-revStr;
	sTemEnd2=(char *)memchr(sTemEnd1+1,' ',revlen);
	//iVarlen=sTemEnd -revStr;
	iVarlen=sTemEnd2-revStr-ipos;
	revlen -=iVarlen;
	memcpy(xmlorgInfo,sTemEnd1+2,iVarlen-3);
	memcpy(revStr,sTemEnd2+1,revlen);
	return 1;
}
void CXmltestDlg::OnButton1() 
{
	char szerrinfo[30],tmpstr[80]={0};
	BOOL iflag;
	ORGITEMINFO phoneinfo={0};
	iflag=GetXmlOrgData(szerrinfo);
	if(iflag)
		MessageBox("解析xml文件成功!");
}

void CXmltestDlg::OnButt_find() 
{
	int iRet=0;
	char keyvalue[8]={0};
	GetDlgItemText(IDC_EDIT_FINDKEY,keyvalue,8);
	//tryfun(keyvalue);
	char sztotalbuf[1024*20]={0};
	if (strlen(keyvalue)==0)
	{
		MessageBox("please input find id:");
		return;
	}
	tryfun(keyvalue,sztotalbuf);	//60001
	MessageBox(sztotalbuf);
}

void CXmltestDlg::OnButt_try()
{

}

void CXmltestDlg::OnButt_find2() 
{
	MyOrg *findOrg;
	char keyvalue[30]={0};
	char sztotalbuf[80]={0};
	//上地人员上地
	//strcpy(keyvalue,"大兴营业部");
	GetDlgItemText(IDC_EDIT_NameQue,keyvalue,30);
	if(strlen(keyvalue)==0)
		return;
	findOrg=FindOrgByName(root,keyvalue);
	if(findOrg!=NULL)
	{
		sprintf(sztotalbuf,"id:%s,name:%s,type:%d",findOrg->m_id,findOrg->m_name,findOrg->m_nType);
		MessageBox(sztotalbuf,"success find!");
	}
}

void CXmltestDlg::OnButtFindid() 
{
	
	MyOrg *findOrg;
	char keyvalue[30]={0};
	char sztotalbuf[80]={0};
	GetDlgItemText(IDC_EDIT_NameQue,keyvalue,30);
	if(strlen(keyvalue)==0)
		return;
	findOrg=FindOrgById(root,keyvalue);
	if(findOrg!=NULL)
	{
		sprintf(sztotalbuf,"id:%s,name:%s,type:%d",findOrg->m_id,findOrg->m_name,findOrg->m_nType);
		MessageBox(sztotalbuf,"success find!");
	}
}

int MyOrg::setkk(int inum)
{

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -