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/d9130a6f09a815884882d3619b200bed to your computer and use it in GitHub Desktop.
Save ballerina-github-bot/d9130a6f09a815884882d3619b200bed to your computer and use it in GitHub Desktop.
examples/xml-to-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[] purchased_item;
@xmldata:Attribute
string 'xmlns?;
@xmldata:Attribute
string status?;
};
@xmldata:Namespace {
uri: "http://example2.com"
}
type Item record {
string itemCode;
int item_count;
};
public function main() returns error? {
xml data = xml `<ns:Invoice xmlns="example.com"
xmlns:ns="http://sdf.com" status="paid">
<id>1</id>
<purchased_item>
<itemCode>223345</itemCode>
<item_count>1</item_count>
</purchased_item>
<purchased_item>
<itemCode>223300</itemCode>
<item_count>7</item_count>
</purchased_item>
</ns:Invoice>`;
// Converts an XML representation to its `record` representation.
Invoice output = check xmldata:fromXml(data);
io:println(output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment