xml spy
Previous  Top  Next
How to handle an event

To handle an event you have to switch to the XMLSpyFormEditor. Use "Tools | Switch to scripting environment" to open the main window of the form editor.

Open the (XMLSpyEvents) module with a double-click at the tree item in the project bar.

Normally all events should be predefined in this module. If the event you want to write code for does not have an entry in the event list box, use the "Add Function" command either from the "Project" menu, or from the popup menu which appears if you right-click the module in the project bar to create it again.

Note that you must use the exact same spelling as was used when XMLSpyFormEditor created the new project and inserted the predefined event handlers for you. You must also fill in possible parameters into the function header. The best way to recreate a deleted event handler is to create a new scripting project and copy name and function definition from there.

The screen shot shows you the (XMLSpyEvents) module and the list of predefined events. Choose On_OpenProject to add some scripting code which will be executed each time XMLSpy opens a project.

scripting_eventsdroplist  

The script sequentially opens all XML files located in the XML folder of the project and validates them. If the validation fails the scripts stops and shows the validation error. If the file passes the validation it will be closed.

We now need to add some code to the On_OpenProject event handler:

function On_OpenProject()
{
   var
   bOK;
   var
   nIndex,nCount;
   var
   objItems,objXMLFolder = null;
   
   objItems = Application.CurrentProject.RootItems;
   nCount = objItems.Count;
   
   // search for XML folder

   for
(nIndex = 0;nIndex < nCount;nIndex++)   {
      var
 txtExtensions;
      txtExtensions = objItems.Item(nIndex).FileExtensions;
      
      if
(txtExtensions.indexOf("xml") >= 0)   {
         objXMLFolder = objItems.Item(nIndex);
         break
;
      }
   }
   
   // does XML folder exist?

   if
(objXMLFolder)   {
      var
 objChild,objDoc;
      
      nCount = objXMLFolder.ChildItems.Count;
      
      // step through associated xml files

      for
(nIndex = 1;nIndex <= nCount;nIndex++)   {
         objChild = objXMLFolder.ChildItems.Item(nIndex);
         
         try
   {
            objDoc = objChild.Open();
            
            // use JScript method to access out-parameters

            var
 strError = new Array(1);
            var
 nErrorPos = new Array(1);
            var
 objBadData = new Array(1);
            
            bOK = objDoc.IsValid(strError,nErrorPos,objBadData);
            
            if
(!bOK)   {
               /
/ if the validation fails, we should display the
               // message from XMLSpy
               // of course we have to create the form "MsgBox" and
               // define the global txtMessage variable
               // 
               // txtMessage = Position:" + nErrorPos[0] + "\n" +             //          strError[0];
               // txtMessage +=   "\n\nXML:\n" + objBadData[0].Name + ", " + 
               //         objBadData[0].TextValue;
               //
               // Application.ShowForm("MsgBox");
               
               break;
            }
               
            objDoc.Close(true
);
            objDoc = null
;
         }
         catch
(Err)   {
            // displaying the error description here is a good idea


            // txtMessage = Err.Description;
   
            // Application.ShowForm("MsgBox");


            break
;
         }
      }
   }
}

That's all. Switch to XMLSpy, open a project and see what happens.


Previous  Top  Next

⌐ 2002 Altova