Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / XML

An XML parser and editor with shades of a Design Pattern

Rate me:
Please Sign up or sign in to vote.
4.86/5 (14 votes)
16 Aug 2010CPOL6 min read 56.3K   2.9K   31  
A very generic XML parser whose internal implementation can be changed without affecting the rest of the source code.
// TXMLReaderView.h : interface of the CTXMLEditorView class
//


#pragma once

#include<afxcview.h>

#include "EditTreeCtrlEx.h"

class CTXMLEditorView;
class CXMLTreeCtrl : public CEditTreeCtrlEx 
{
public:
	CXMLTreeCtrl()
	{
		_bNewParent = false;
		_bAttrib = false;
		_view = NULL;
	}

	void SetParentView(CTXMLEditorView* view)
	{
		_view = view;
	}
	
	protected:
		virtual void OnNewItem(HTREEITEM hItem);
		virtual bool NewItem(TVINSERTSTRUCT& ins);
		virtual bool DoDeleteItem(HTREEITEM hItem);		
		
public:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnTvnBeginlabeledit(NMHDR *pNMHDR, LRESULT *pResult);	
public:
	afx_msg void OnTvnDeleteitem(NMHDR *pNMHDR, LRESULT *pResult);

private:	
	CTXMLEditorView* _view;
	
	bool _bNewParent;
	bool _bAttrib;

protected:
	virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
};


class CTXMLEditorView : public CView
{
protected: // create from serialization only
	CTXMLEditorView();
	DECLARE_DYNCREATE(CTXMLEditorView)

// Attributes
public:
	CTXMLEditorDoc* GetDocument() const;
	CXMLTreeCtrl& GetTreeCtrl()
	{
		return m_treeCtrl;
	}	
	
	void SetModified(BOOL modified);
	

// Operations
public:
	void PopulateTreeCtrl(TXMLParser& parser, TXMLNode* curNode, HTREEITEM hParent);
// Overrides
public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// Implementation
public:
	virtual ~CTXMLEditorView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:
	CXMLTreeCtrl m_treeCtrl;
	void UpdateXMLDocument();
// Generated message map functions
protected:
	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, LPVOID lpParam = NULL);
public:
	afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
	afx_msg void OnFilePrintPreview();	
public:
	afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
public:
	afx_msg void OnPaint();
protected:
	virtual void OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/);
public:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
public:
	afx_msg void OnSize(UINT nType, int cx, int cy);
public:
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
public:
	virtual void OnFinalRelease();
public:
	afx_msg void OnFileSave();
public:
	afx_msg void OnFileSaveAs();
public:
	afx_msg void OnUpdateFileSaveAs(CCmdUI *pCmdUI);
public:
	virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
	void FormXMLDocFromTree(HTREEITEM hParent, TXMLNode* parent);
};

#ifndef _DEBUG  // debug version in TXMLReaderView.cpp
inline CTXMLReaderDoc* CTXMLEditorView::GetDocument() const
   { return reinterpret_cast<CTXMLReaderDoc*>(m_pDocument); }
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

Comments and Discussions