This section explains how to create a new Form in the XMLSpyFormEditor and how to display it in XMLSpy. The sample builds a dialog where the user can type in the name of elements that are to be deleted from the active XML file in XMLSpy.
To create a form, switch to the XMLSpyFormEditor. Use "Tools | Switch to scripting environment" to open the main window of the form editor. There is at least one project open, the global project.
There are two ways for adding a new form to a project. You can use the "Add Form" command from the "Project" menu, or click the "New Form" Button in the project window. In both cases, the new form is added immediately and appears as a separate window.
The screen shot shows the XMLSpyFormEditor right after the creation of a new form:
You could change the name of the new form now. To do so, enter a new name in the Property Sheet in the line (FormCode). For our example type in "RemoveDlg". Type in the form-title you like in the "Title" line of the Property Sheet.
We then insert an edit box where the user can type in the name of the elements to remove from the XML file. Select the edit box icon
from the tool bar on the right side of the main window. Define a rectangle with the mouse for the edit box in the display area of our RemoveDlg form.
In the (ObjectCode) line of the Property Sheet, type in "EditElements" as the name of the new edit box. Add a caption for the edit box with the static text icon
from the tool bar. The edit window of the form should look like this:
We now need two buttons in our form, "Delete" and "Cancel". Use the button icon
to draw them in our form window. Type in "BtnDelete" and "BtnCancel" as names in the (ObjectCode) lines of the button properties.
Set the button text in the Text fields to "Delete" and "Cancel". Additionally set the type of the Cancel - Button to "0 - OK" in the ButtonType field of the properties.
After we have designed our form we need some scripting code to make it useful.
We need a function that recursively steps through the XML file, and deletes all XMLData objects with a given name. We declare this in the (GlobalDeclarations) module of the project. If the module is not visible, double-click it in the project bar. Type in the following code:
var txtElementName;
function DeleteXMLElements(objXMLData)
{
if(objXMLData == null)
return;
if(objXMLData.HasChildren) {
var objChild;
objChild = objXMLData.GetFirstChild(-1);
while(objChild) {
DeleteXMLElements(objChild);
try {
if(objChild.Name == txtElementName)
objXMLData.EraseCurrentChild();
objChild = objXMLData.GetNextChild();
}
catch(Err) {
objChild = null;
}
}
}
}
After this your global declarations module should look like this:
Select the BtnDelete button and choose the "Events" tab from the property sheet. Click on the edit code icon
in the "EventClick" line of the tab. The Mini-Editor window pops up with the predefined function header of the click-event. This function will be executed every time the user clicks the "Delete" button. Add the following code to the event:
if(EditElements.Text != "") {
txtElementName = EditElements.Text;
DeleteXMLElements(Application.ActiveDocument.RootElement);
}
The Delete - Button sets the global variable txtElementName to the string from the edit box and calls DeleteXMLElements() with the root element of the active document.
There is not much left to do.
Select the (XMLSpyMacros) module, create a new macro (see also "How to write a macro") called "DeleteElements" and enter this code:
var a;
if(Application.ActiveDocument != null)
a = Application.ShowForm("RemoveDlg");
Run the macro from the "Show macros..." dialog of the "Tools" menu within XMLSpy.
Previous
Top