Example Java Project
The XMLSpy installation package contains an example Java project, located in the the API\Java subfolder of the Examples folder :
Windows 7, Windows 8, Windows 10, Windows 11 | C:\Users\<username>\Documents\ |
This folder contains Java examples for the XMLSpy API. You can test it directly from the command line using the batch file BuildAndRun.bat, or you can compile and run the example project from within Eclipse. See below for instructions on how to use these procedures.
File list
The Java examples folder contains all the files required to run the example project. These files are listed below. If you are using a 64-bit version of the application, some filenames contain _x64 in the name. These filenames are indicated with (_x64).
AltovaAutomation(_x64).dll | Java-COM bridge: DLL part |
AltovaAutomation.jar | Java-COM bridge: Java library part |
XMLSpyAPI.jar | Java classes of the XMLSpy API |
RunXMLSpy.java | Java example source code |
BuildAndRun.bat | Batch file to compile and run example code from the command line prompt. Expects folder where Java Virtual Machine resides as parameter. |
.classpath | Eclipse project helper file |
.project | Eclipse project file |
XMLSpyAPI_JavaDoc.zip | Javadoc file containing help documentation for the Java API |
What the example does
The example starts up XMLSpy and performs a few operations, including opening and closing documents. When done, XMLSpy stays open. You must close it manually.
•Start XML Spy: Starts XMLSpy, which is registered as an automation server, or activates XMLSpy if it is already running.
•Open OrgChart.pxf: Locates one of the example documents installed with XMLSpy and opens it.
•Iteration and Changing the View Mode: Changes the view of all open documents to Text View. The code also shows how to iterate through open documents.
•Iteration, validation, output parameters: Validates the active document and shows the result in a message box. The code shows how to use output parameters.
•Event Handling: Shows how to handle XMLSpy events.
•Shut down XMLSpy: Shuts down XMLSpy.
You can modify the example in any way you like and run it.
Running the example from the command line
To run the example from the command line, open a command prompt window, go to the Java folder of the API Examples folder (see above for location), and then type:
buildAndRun.bat "<Path-to-the-Java-bin-folder>"
The Java binary folder must be that of a JDK 14 or later installation on your computer. Press the Return key. The Java source in RunXMLSpy.java will be compiled and then executed.
Loading the example in Eclipse
Open Eclipse and use the Import | Existing Projects into Workspace command to add the Eclipse project file (.project) located in the Java folder of the API Examples folder (see above for location). The project RunXMLSpy will then appear in your Package Explorer or Navigator. Select the project and then the command Run as | Java Application to execute the example.
Note: | You can select a class name or method of the Java API and press F1 to get help for that class or method. |
Java source code listing
The Java source code in the example file RunXMLSpy.java is listed below with comments.
001 // Access general JAVA-COM bridge classes
002 import com.altova.automation.libs.*;
003
004 // Access XMLSpy Java-COM bridge
005 import com.altova.automation.XMLSpy.*;
006 import com.altova.automation.XMLSpy.Enums.SPYViewModes;
007
008 /**
009 * An example that starts XMLSpy COM server and performs view operations on it
010 * Feel free to extend
011 */
012 public class RunXMLSpy
013 {
014 public static void main(String[] args)
015 {
016 // An instance of the application.
017 Application xmlSpy = null;
018
019 // Instead of COM error handling, use Java exception mechanism
020 try
021 {
022 // Start XMLSpy as COM server
023 xmlSpy = new Application();
024
025 // COM servers start up invisible, so make it visible
026 xmlSpy.setVisible(true);
027
028 // Locate samples installed with the product
029 String strExamplesFolder =
030 System.getenv("USERPROFILE") + "\\My Documents\\Altova\\XMLSpy2012\\Examples\\";
031
032 // Open two example files
033 xmlSpy.getDocuments().openFile(strExamplesFolder + "OrgChart.pxf", false);
034 xmlSpy.getDocuments().openFile(strExamplesFolder + "ExpReport.xml", false);
035
036 // Iterate through open documents and set view mode to 'Text'.
037 for (Document doc:xmlSpy.getDocuments())
038 if ( doc.getCurrentViewMode() != SPYViewModes.spyViewText)
039 doc.switchViewMode(SPYViewModes.spyViewText);
040
041 // An alternative iteration mode is index-based
042 // COM indices are typically zero-based
043 Documents documents = xmlSpy.getDocuments();
044 for (int i = 1; i <= documents.getCount();
045 i++)
046 {
047 Document doc = documents.getItem(i);
048
049 // Validation is one of the few methods to have output parameters.
050 // The class JVariant is the correct type for parameters in these cases.
051 // To get values back mark them with the by-reference flag.
052 JVariant validationErrorText = new
053
054 JVariant.JStringVariant("");
055
056 validationErrorText.setByRefFlag();
057 JVariant validationErrorCount = new
058
059 JVariant.JIntVariant(0);
060
061 validationErrorCount.setByRefFlag();
062 JVariant validationErrorXMLData = new
063
064 JVariant.JIDispatchVariant(0);
065
066 validationErrorXMLData.setByRefFlag();
067 if (!doc.isValid(validationErrorText, validationErrorCount, validationErrorXMLData))
068 System.out.println("Document" + doc.getName() + " is not wellformed - " + validationErrorText.getStringValue());
069 else
070 System.out.println("Document" + doc.getName() + " is wellformed.");
071 }
072
073 // The following lines attach to the document events using a default implementation
074 // for the events and override one of its methods.
075 // If you want to override all document events it is better to derive your listener class
076 // from DocumentEvents and implement all methods of this interface.
077 Document doc = xmlSpy.getActiveDocument();
078 doc.addListener(new
079
080 DocumentEventsDefaultHandler()
081 {
082 @Override
083 public boolean
084
085 onBeforeCloseDocument(Document i_ipDoc) throws AutomationException
086 {
087 System.out.println("Document
088
089 " + i_ipDoc.getName() + " requested closing.");
090
091 // Allow closing of document
092 return true;
093 }
094 });
095 doc.close(true);
096 doc = null;
097
098 System.out.println("Watch XMLSpy!");
099 }
100 catch (AutomationException e)
101 {
102 // e.printStackTrace();
103 }
104 finally
105 {
106 // Make sure that XMLSpy can shut down properly.
107 if (xmlSpy != null)
108 xmlSpy.dispose();
109
110 // Since the COM server was made visible and still is visible,
111 // it will keep running, and needs to be closed manually.
112 System.out.println("Now close XMLSpy!");
113 }
114 }
115 }