PDA

View Full Version : using XML to save menus?!?!



nupul
7th April 2006, 06:29
Is it possible to save "menu definitions" in an Xml file and then read them and implement the menu of the MainWindow?

Assume the following definition (well formed XML):



//<DTD etc goes here>

<menu name="file">
<menuItem>New</menuItem>
<submenu name="preferences">
<submenuItem>A</submenuItem>
<submenuItem>B</submenuItem>
</submenu>
</menu>



How would I read this file and implement a menu out of this...I went through the doc but am not v.clear as to the syntax to use to implement a menu out of the XML file!

:confused:

Thanks in advance!

Nupul

munna
7th April 2006, 06:52
To build menu from your xml file:

Using QDomElement read each element of your xml file. Each element that do not have any child is a QAction which is added to menu, whereas element which has one or more children is QMenu which comes under its parent (if it has one).

You might have to maintain the list of actions and menus so that you can build xml again from your menus.

nupul
7th April 2006, 07:04
what is preferable...SAX or DOM and why?

munna
7th April 2006, 07:26
SAX is an event-based standard interface for XML parsers. The Qt interface follows the design of the SAX2 Java implementation. Its naming scheme was adapted to fit the Qt naming conventions. Details on SAX2 can be found at http://www.saxproject.org.

DOM Level 2 is a W3C Recommendation for XML interfaces that maps the constituents of an XML document to a tree structure. Details and the specification of DOM Level 2 can be found at http://www.w3.org/DOM/. More information about the DOM classes in Qt is provided in the Qt DOM classes.

Brandybuck
7th April 2006, 08:22
what is preferable...SAX or DOM and why?
It's largely up to you, and is a matter of preference. They are two distinct ways of interacting with XML. I personally prefer DOM because I can treat the XML as a data structure, instead of a stream of events.

wysota
7th April 2006, 09:32
DOM will be better in this case. In is superior to SAX in its capabilities and its only drawback is that the whole tree has to be loaded into memory (which is not the case with SAX). But I guess you won't have a dozen megabyte tree for your menus so it's not a drawback in your case.