Skip to content

Instantly share code, notes, and snippets.

@ballerina-github-bot
Created September 25, 2023 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ballerina-github-bot/358a01c41a5409460c72a4c1500470f0 to your computer and use it in GitHub Desktop.
Save ballerina-github-bot/358a01c41a5409460c72a4c1500470f0 to your computer and use it in GitHub Desktop.
examples/xml-from-record-conversion
import ballerina/io;
import ballerina/xmldata;
// Defines a record type with annotations.
@xmldata:Namespace {
prefix: "ns",
uri: "http://sdf.com"
}
type Invoice record {
int id;
Item[] items;
@xmldata:Attribute
string 'xmlns = "example.com";
@xmldata:Attribute
string status?;
};
@xmldata:Namespace {
uri: "http://example1.com"
}
type Item record {
string itemCode;
int count;
};
public function main() returns error? {
// Creates an `Invoice` record.
Invoice data = {
id: 1,
items: [
{itemCode: "223345", count: 1},
{itemCode: "223300", count: 7}
],
status: "paid"
};
// Converts a `record` representation to its XML representation.
xml result = check xmldata:toXml(data);
io:println(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment