Create and open files
Use the methods of the "Documents" interface to create and open documents. This interface is accessible via the "Application.Documents" property.
Example:
Dim objDoc As Document
Set objDoc = objSpy.Documents.OpenFile("C:\xmlfiles\myfile.xml", False)
Sometimes it is necessary to show a file dialog to the user to select a file. This usually happens in the scripting environment and you only need to pass True as the second parameter to OpenFile().
To create a new file use "Documents.NewFile()". Any existing file with the same path will be overwritten during the next Document.Save() call.
Documents collection
All open documents are accessible via the "Documents" collection. All collection objects in the XMLSpy API conform to the Microsoft specifications. So it is possible to use the For-Each loop in VisualBasic to iterate through the open documents.
Example:
Dim objDocs As Documents
Dim objDoc As Document
Set objDocs = objSpy.Documents
For Each objDoc In objDocs
'do something useful with your document
objDoc.AssignXSL "C:\myXSL.xsl", False
Next
Another way to access a document is the "Documents.Item" method. The method takes an index as a parameter and gets the corresponding document object from the collection. Please note that 0 (zero) is the first index value. "Documents.Count" retrieves the total number of documents.
Example:
Dim objDocs As Documents
Dim objDoc As Document
Set objDoc = objDocs.Item(1) 'gets the first document
Validation
One common task on documents is to validate them against an assigned schema or DTD. If the XML file has no schema or DTD already assigned, use "Document.AssignSchema" or "Document.AssignDTD" to add the necessary references to the document.
Examples:
objSpy.ActiveDocument.AssignSchema "C:\mySchema.xsd", False
or
objSpy.ActiveDocument.AssignDTD "C:\myDTD.dtd", False
If you want the user to select a schema or DTD, pass True as the second parameter to these functions to display a file-dialog. These methods only put the reference into the document and do not check the existence of the specified file. If the file path is not valid, the validation will fail.
After you have assigned a valid schema or DTD reference to your file, you are able to validate it with "Document.IsValid". IsValid needs some out-parameters that must be declared as VARIANTs to be accessible from script languages like VBScript and JavaScript.
Example:
Dim bValid As Boolean
Dim strMsg As Variant
Dim nPos As Variant
Dim objBadXMLData As Variant
bValid = objSpy.ActiveDocument.IsValid(strMsg, nPos, objBadXMLData)
If bValid = False Then
a = MsgBox("The document is not valid:" & Chr(13) &
strMsg & Chr(13) & "position: " & nPos &
Chr(13) & "XMLData name: " & objBadXMLData.Name)
Else
a = MsgBox("The document is valid")
End If
Previous
Top
Next