Skip to content

Builder API (Java to XML)

The MX messages builder API is provided to create new MX messages from Java, and serialize its content into XML.

The creation of a new message basically consists of creating an object that represents the message, and the subsequent addition of inner elements.

The order is not important because the model is already constrained to the correspondent tree structure of the XML. Although this does not mean the builder API will verify validation constraints of the message but the model will naturally allow only proper elements to be appended.

This Sample code shows how to create an MX ACMT.001.001.03 message:

    MxAcmt00100103 mx = new MxAcmt00100103()
            .setAcctOpngInstr(new AccountOpeningInstructionV03()
                    .setAcctPties(new AccountParties6()
                            .setAdmstr(new InvestmentAccountOwnershipInformation6()
                                    .setClntId("clntId"))
                            .setPrncplAcctPty(new AccountParties1Choice()
                                    .setNmnee(new InvestmentAccountOwnershipInformation6()
                                            .setPty(new Party14Choice().setOrg(new Organisation13().setNm("orgName"))
                                            )
                                    )
                            )
                    )
            );
    String mxXml = mx.message();
    System.out.println(mxXml);

To simplify the resulting builder code, all setters in the business dictionary allow chained method calls. Also inner element class names match the exact names as defined in the MX standard documentation.

The resulting XML is:

<?xml version="1.0" encoding="UTF-8" ?>
<Doc:Document xmlns:Doc="urn:swift:xsd:acmt.001.001.03">
    <Doc:AcctOpngInstr>
        <Doc:AcctPties>
            <Doc:PrncplAcctPty>
                <Doc:Nmnee>
                    <Doc:Pty>
                        <Doc:Org>
                            <Doc:Nm>orgName</Doc:Nm>
                        </Doc:Org>
                    </Doc:Pty>
                </Doc:Nmnee>
            </Doc:PrncplAcctPty>
            <Doc:Admstr>
                <Doc:ClntId>clntId</Doc:ClntId>
            </Doc:Admstr>
        </Doc:AcctPties>
    </Doc:AcctOpngInstr>
</Doc:Document>

Customization

The builder API is designed to be flexible and allow customization of the resulting XML. This example shows how to customize the previous result.

    MxAcmt00100103 mx = new MxAcmt00100103()
            .setAcctOpngInstr(new AccountOpeningInstructionV03()
                    .setAcctPties(new AccountParties6()
                            .setAdmstr(new InvestmentAccountOwnershipInformation6()
                                    .setClntId("clntId"))
                            .setPrncplAcctPty(new AccountParties1Choice()
                                    .setNmnee(new InvestmentAccountOwnershipInformation6()
                                            .setPty(new Party14Choice().setOrg(new Organisation13().setNm("orgName"))
                                            )
                                    )
                            )
                    )
            );
    MxWriteConfiguration conf = new MxWriteConfiguration();
    conf.documentPrefix = null; // remove the default Doc: prefix
    conf.indent = " "; // use 1 space for indentation
    conf.includeXMLDeclaration = false; // remove the default XML declaration
    String mxXml = mx.message(conf);
    System.out.println(mxXml);

This will result in the following XML:

<Document xmlns="urn:swift:xsd:acmt.001.001.03">
 <AcctOpngInstr>
  <AcctPties>
   <PrncplAcctPty>
    <Nmnee>
     <Pty>
      <Org>
       <Nm>orgName</Nm>
      </Org>
     </Pty>
    </Nmnee>
   </PrncplAcctPty>
   <Admstr>
    <ClntId>clntId</ClntId>
   </Admstr>
  </AcctPties>
 </AcctOpngInstr>
</Document>

Refer to the MxWriteConfiguration javadoc to review all the available customization options.