XML DOM Serializer and Deserializer

Use DOMSerializer and DOMDeserializer to serialize and deserialize Java XML DOM object to and from the Candid payload of type RECORD.

A fully functional example using DOMSerializer and DOMDeserializer can be found here.

This example uses Motoko to call the canister main.mo. The canister uses 2 complex types, LoanApplication and LoanOffer.

main.mo
// Loan Application
public type LoanApplication = {
    id: Nat;
    firstname: Text;
    lastname: Text;
    zipcode: Text;
    ssn: Text;
    amount: Float;
    term: Nat16;
    created: Int;
 };

// Loan Offer
public type LoanOffer = {
    providerid: Principal;
    providername: Text;
    applicationid: Nat;
    apr: Float;
    created: Int;
};

The canister has 2 methods: apply and getOffers.

The example uses the file with XML LoanApplication payload as an input.

Next, create IDLValue using the DOMSerializer create method.

The Serializer input is the variable type DOM Element.

To be able to properly map names and values to Candid name types for DOMDeserializer, declare the IDLType structure as follows:

Use UpdateBuilder, QueryBuilder or Raw Methods to call the Canister and deserialize output to DOM Element. Function rootElement is used to define root element of the XML structure. To set Candid XML Attributes in XML output use setAttributes function with true value.

By default, DOMDeserializer generates qualified XML document with namespaces. To generate unqualified XML document use DOMDeserializer function setQualified with false value.

XSI Types

DOMSerializer can use XML XSI attribute to convert XML value to specific primitive Candid type.

XSI XML namespace is http://www.w3.org/2001/XMLSchema-instance. Type is defined as XML Schema type http://www.w3.org/2001/XMLSchema.

Here is mapping table between Candid and XML Schema.

Candid
XML Schema

bool

boolean

int

integer

int8

byte

int16

short

int32

int

int64

long

nat

posiiveInteger

nat8

unsignedByte

nat16

unsignedShort

nat32

unsignedInt

nat64

unsignedLong

float32

float

float64

double

text

string

principal

ID

Using Candid XML Attributes

The developer can also use Candid XML attributes name and type to define Candid name and type. If XML document is qualified then it requires candid namespace definition http://ic4j.org/candid.

Handling XML Arrays

By default, DOMSerializer and DOMDeserializer will assume that name of array item is item. To modify this name use function arrayItem in DOMSerializer and DOMDeserializer.

Last updated