MessageObjects Tutorial: SWIFT to XML
The following tutorial will guide
you through some typical usage examples.
Only 2 message types are used for all examples: MT200
and MT502. MT200 is extremely simple, MT502 is an ISO15022
type message and is quite complex (containing sequences
and repetitions).
Please note that the tutorial was created with the .NET
version of MessageObjects, which may vary slightly from
the Java version.
II. Swift to XML
Step 1: Converting SWIFT to XML
- In our .NET project, we have set a reference to SWIFTXMLnet.dll.
- We have created an instance sx of SWIFTXMLnet.SXML,
- We used sx.setVersionPathName("C:\MessageObjects\Standards2003") to point to the correct location.
- Now we can use sx.swift2xml(TextArea) to create
an XML document from the data pasted into field "TextArea".
Test it by pasting an MT200 or MT502 into the field below and then click the button "GO".
Here you will either see the generated XML or an error message (such as "unable to parse..." if the message did not follow SWIFT rules accurately enough to be transformed). (The XML was retrieved by calling sx.getXMLDoc.)
When you look at the returned XML, you will see that
nodes are identified by an opening tag and a closing
tag, such as <Block2> and </Block2>.
If the node has attributes, they are visible inside
the opening tag, like this: <Block2 MT="200">.
(The node Block2 has an attribtue MT with the value
"200".)
Often the attributes are not visible, because they are default-attributes which are defined in the DTD. However, they are there and can be located and used from program code. MessageObjects uses a set of attributes to provide information on the SWIFT structure and data types.
Step 2: Displaying SWIFT Messages as HTML
Follow Step 1, so that you have an
XML representation of a SWIFT message loaded into the
API.
Select an XSLT from the list and click button "GO"
- this will place a call to sx.getHTMLView, passing
the name of the XSLT as first variable to the function.
If you apply view2.xsl, then you can (but don't have
to) pass 3 parameters:
grpsep, decsep and testdate which provide location dependant
editing. Default values are
grpsep=' (you can use a different separator,
e.g. , )
decsep=. (you can use a different decimal
point, e.g. , but then you should not use , for group
separator, but maybe . instead)
testdate=31.12.2002 23:59:59 (use the
same date but in different notation, e.g. 12/31/2002
23.59.59)
If you apply edit2.xsl with MT502, then you can also set NodeKey to display a different portion of the message.
Step 3: Transforming from one XML to another XML Layout
This step is the quite the same as the previous one, the only difference being that the XSLT creates XML instead of HTML.
Follow Step 1, so that you have an
XML representation of a SWIFT message loaded into the
API.
Select an XSLT from the list and click button "GO"
- this will place a call to sx.getHTMLView, passing
the name of the XSLT as first variable to the function.
The resulting XML can be used as input to iTextSharp, an Open Source project for creating PDF.
Of course you can create your own XSLT to transform into your own XML or Text format (e.g. a fixed record layout for file input).
Step 4: Analysing and using SWIFT Messages in XML Format
In the previous step you saw how to transform a message with XSLT. Perhaps you don't want to use XSLT but prefer to directly use code in your program. Whichever is the case, if you want to, for example, treat incoming SWIFT messages for use in an application, you will need to understand how MessageObjects structures the XML representation of the SWIFT message. See the .NET API Reference Overview for details.
It is important to understand that the information
about the XML nodes is found in the attributes.
You can use XPath to directly access information, e.g.
- go back to step 1 and convert an MT200.
- use the XPath statement:
"//*[@Tag='20' and @SubField='1']"to retrieve the Transaction Reference of the message. - Code snippet for VB.NET:
Dim myxml as System.Xml.XmlDocument
Dim mynode as System.Xml.XmlNode
myxml=sx.getXMLDoc()
mynode=myxml.SelectSingleNode("//*[@Tag='20' and @SubField='1']")
If Not mynode is Nothing then MsgBox("This is the Transaction Reference: " & mynode.innerText)
End If - Code snippet for XSLT, this is the Transaction
Reference: <xsl:select="//*[@Tag='20' and @SubField='1']"/>
Step 5: Creating Version-Robust Code
In the previous step, we used XPath to locate the node
with both attributes Tag="20"
and SubField="1".
You may have noticed that the "Key" attribute
exists on every node and is always unique - therefore
the statement "//*[@Key='AA_1_20_1']"
would guarantee simple retrieval of the exact value
(some message types have more than one occurrence of
Field 20!).
There is, however, a problem here. SWIFT unfortunately
makes changes to standards on a yearly basis.
As the Key value depends on the location of the node,
any change in the message structure may also change
the value of the Key attribute. It is therefore worth
the extra effort of referring to nodes by their relative
location rather than by their Key. In order to get the
Transaction Reference from an MT502, the XPath could
be "//*[@OType='S' and @Tag='A']//*[@Tag='20C'
and @SubField='2']".
Another approach could be to build a nodelist and step through it as in the example below.
