Re: XML editor using MFC
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Mon, 03 Dec 2007 15:15:11 -0500
Parse the XML. As you are parsing, add elements to the tree control. Each node of the
XML tree is represented by an HTREEITEM. It should take about two hours to write this
code from scratch, except for the CDATA stuff which is a real pain to parse. If you've
never written a lexer or parser, plan on about a day.
If your XML files are < 100MB, don't even bother with reading from the disk; allocate a
buffer the size of the file and read the whole contents in at once, or use a memory-mapped
file and and map the whole file into memory. Saves a lot of nonsense about reading files,
which just gets in the way. One thing you pass into the recursion is a FILEINFO structure
of your own invention but essentially it needs to track line# and character position for
error reporting. No global variables are required; one way to be sure you are making an
error in the coding is if you use any global or static variables.
Parsing XML is trivial. You need to look for < and >, handle <name:name /> and
<name>...</name> structures, all values are of the form keyword="value", which is real
easy to parse. When you find a <, add a new node to the TreeItem you are currently
working on, and call the parser recursively.
Error recovery: simplest form, when you find an error, throw a CException-derived class,
e.g.,
throw new CXMLSyntaxError(line, character);
where line and character are file positions. It's horrible error recovery, but
sophisticated error recovery will take weeks to implement, and is nearly impossible to get
right anyway. You catch it at the topmost level surrounding the first call.
You can use different icons for the ImageList for nodes and values; in fact you need three
icons: a Node icon, a ValueName icon, and a Value icon.
If you download the code example for WIn32 programming, you can find examples of how to
drag items around in a tree view (its sort of a weird hybrid of C and MFC, but it shows
the example clearly. If you parse the DTD, you can determine what nodes are allowed to be
dragged to other nodes, but now you're talking some real investment of time, several days
at least)
Write a simple lexer using a simple FSM model. There are several examples of this on my
MVP Tips site. When you find a <, enter the parser recursively, passing in a newly-added
HTREEITEM that represents the node into which you are putting the information.
joe
On Mon, 3 Dec 2007 10:23:52 -0800 (PST), adiideas@xxxxxxxxx wrote:
Hello,Joseph M. Newcomer [MVP]
I need to develop XML editor using MFC. It needs to feature Tree View
of XML.
And have to use MFC language.
Could you please let me know any pointers?
TIA
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: XML editor using MFC
- From: David Lowndes
- Re: XML editor using MFC
- References:
- XML editor using MFC
- From: adiideas
- XML editor using MFC
- Prev by Date: Re: Passing the id to a function
- Next by Date: Re: MFC updates and enhancements
- Previous by thread: Re: XML editor using MFC
- Next by thread: Re: XML editor using MFC
- Index(es):
Relevant Pages
|