Sample source code (obsolete)
The objects described in this section (Application API for Java) are obsolete from v2012 onwards.
For information about how to access the Application API from Java code, see the section: Programming Languages | Java. |
The "SpyDoc doc = app.GetDocuments().OpenFile(...)" command parameter must be altered to suit your environment.
What the sample does:
•Starts a new XMLSpy instance
•Opens the Datasheet.xml file (alter the path here...)
•Switches to the Enhanced Grid view
•Appends a new child element called "NewChild" with the text value "NewValuE" element to the root element
•Checks if the document is valid and outputs a message to the Java console
•Quits and releases the XMLSpy application
import XMLSpyInterface.*;
public class TestSpyInterface
{
public TestSpyInterface() {}
public static void main(String[] args)
{
SpyApplication app = null;
SpyDoc oDoc = null;
SpyXMLData oData = null;
SpyXMLData oNewChild = null;
try
{
app = new SpyApplication();
app.ShowApplication( true );
oDoc = app.GetDocuments().OpenFile("C:\\FilePath\\OrgChart.xml", true );
// OrgChart.xml is in the folder C:\Documents and Settings\<username>\My Documents\Altova\XMLSpy2025. The filepath should be in
// the form: C:\\Documents and Settings\\Username\\Folder\\Filename.xml
if ( oDoc != null )
{
oDoc.SwitchViewMode(SPYViewModes.spyViewGrid);
oData = oDoc.GetRootElement();
oNewChild = oDoc.CreateChild(SPYXMLDataKind.spyXMLDataElement);
oNewChild.SetName( "NewChild" );
oNewChild.SetTextValue("newVaLuE");
oData.AppendChild(oNewChild);
if ( oDoc.IsValid() == false )
{
// is to be expected after above insertion
System.out.println( "!!!!!!validation error: " + oDoc.GetErrorString() );
System.out.println( "!!!!!!validation error: " + oDoc.GetErrorPos() );
System.out.println( "!!!!!!validation error: " + oDoc.GetBadData() );
}
}
app.Quit();
}
finally
{
// Free any allocated resources by calling ReleaseInstance().
if ( oNewChild != null )
oNewChild.ReleaseInstance();
if ( oData != null )
oData.ReleaseInstance();
if ( oDoc != null )
oDoc.ReleaseInstance();
if ( app != null )
app.ReleaseInstance();
}
}
}
If you have difficulties compiling this sample, please try the following commands on the (Start | Run | cmd) command line. Please make sure you are currently in the folder that contains the sample java file.
compilation
javac -classpath c:\yourpathhere\XMLSpyInterface.jar testspyinterface.java
Execution
java -classpath c:\yourpathhere\XMLSpyInterface.jar testspyinterface