Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
XML to Record conversion
The data.xmldata
library provides multiple APIs to perform the conversion from XML source, which can be provided as a string
, byte array
, byte block stream
, or xml
, into a Ballerina record.
For more information on the underlying module, see the data.xmldata
module.
import ballerina/data.xmldata;
import ballerina/io;
type Book record {
int id;
string title;
string author;
};
xml xmlData = xml `
<book>
<id>10024</id>
<title>Clean Code</title>
<author>Robert C. Martin</author>
</book>`;
string xmlStr = string `
<book>
<id>10024</id>
<title>Clean Code</title>
<author>Robert C. Martin</author>
</book>`;
public function main() returns error? {
// Convert the XML value to a record type.
Book book1 = check xmldata:parseAsType(xmlData);
io:println(book1);
// Convert the XML string to a record type.
Book book2 = check xmldata:parseString(xmlStr);
io:println(book2);
byte[] xmlByteArr = xmlStr.toBytes();
// Convert the XML byte array to a record type.
Book book3 = check xmldata:parseBytes(xmlByteArr);
io:println(book3);
stream<byte[], error?> byteBlockStream = new (new ByteBlockGenerator(xmlStr));
// Convert the XML byte block stream to a record type.
Book book4 = check xmldata:parseStream(byteBlockStream);
io:println(book4);
}
// Defines a class called `ByteBlockGenerator`, which implements the `next()` method.
// This will be invoked when the `next()` method of the stream gets invoked.
class ByteBlockGenerator {
private int index = 0;
private final byte[] byteArr;
private final int arraySize;
public function init(string data) {
self.byteArr = data.toBytes();
self.arraySize = self.byteArr.length();
}
public isolated function next() returns record {|byte[] value;|}|error? {
if self.index >= self.arraySize {
return;
}
int startIndex = self.index;
self.index = startIndex + 4 > self.arraySize ? self.arraySize : startIndex + 3;
return {value: self.byteArr.slice(startIndex, self.index)};
}
}
$ bal run xml_to_record.bal{"id":10024,"title":"Clean Code","author":"Robert C. Martin"}{"id":10024,"title":"Clean Code","author":"Robert C. Martin"}{"id":10024,"title":"Clean Code","author":"Robert C. Martin"}{"id":10024,"title":"Clean Code","author":"Robert C. Martin"}
PreviousXMLNS declarations
NextXML to record with projection