Useful XmlHandler time saver

I made this class to help save myself time seeing as most of my applications handle XML in some way.

For a quick way of reading and writing xml files, simply inherit this XmlHandler class into your base component, or wherever else you feel it relevant.

It gives your component the following functions:


	XmlElement* getXml()

	void setXml (XmlElement* newXml)

	void setFileTypes( const String &newTypes )

	void setAnyFileTypes()

	XmlElement* openXmlFileWithBrowser()

	void saveXmlFileWithBrowser()

	virtual void parseXml()

It gives you two file access functions that open a browser -

openXmlFileWithBrowser();

saveXmlFileWithBrowser();

The ‘open’ function will read in the chosen file and store the root xml element in the internal xml pointer (accessed with getXml() ).

The ‘save’ function will write the xml structure held in the pointer to the target file chosen in the pop-up dialog. This means you can resave the updated contents of the structure, or simply set the pointer to a new xml structure (with setXml(XmlElement*) ) and write that.

You can set the ‘file types’ filtering before you perform either of these functions with the setFileTypes(String) function. For example:

setFileTypes( "*.xml" );

This will cause the dialog to only show files with an .xml extension. The format of this string is just how it is normally used with a juce FileChooser.

The ‘open’ function automatically calls the virtual function parseXml() once the file has been read and stored into the xml pointer. Simply override this function, and you can specify how the newly read xml data is manipulated on loading. Thus, it will automatically extract the information you want from it if you tell it how to do so here.

I find this very handy to have around, and thought it may come in handy for some of you guys.

Here’s the header (just #include “XmlHandler.h” and then inherit it into your class).
http://haydxn.net/content/JUCE/XmlHandler.h

Really basic example of how to include it…




class XmlHandlingComponent : public Component, public XmlHandler
{
   ...
   void parseXml()
   {
      // read the root tag name on opening...
      String name = getXml()->getTagName();
   }
}

Make your component an ActionListener and call the open/save functions in response to messages from ‘open’ and ‘save’ TextButtons. Simple as that.

Updated it a bit! :slight_smile:

Now there is a way of accessing the File details (foolishly omitted from the first version!)

const File& getLastInputFile()
const File& getLastOutputFile()

These return the last opened / saved file details, so that you can extract the file name and use that in your program.

The following code could be used to respond to an ‘open’ button press via the actionListenerCallback() function:

	setFileTypes( "*.xml" );
	openXmlFileWithBrowser();
	File fileJustOpened = getLastInputFile();
	if ( fileJustOpened != File::nonexistent )
	{
		fileNameLabel->setText(T("OPEN FILE: ") + fileJustOpened.getFileName() );
	}

There is also a separate setSaveType(String) function to set the type used for the save dialog (independently from the open dialog).

The link for the header is the same as in the first post.