Swan Lake Beta2 is here.
import ballerina/io;

public function main() {
    // An XML element. There can be only one root element.
    xml x1 = xml `<book>The Lost World</book>`;
    io:println(x1);

    // An XML text.
    xml x2 = xml `Hello, world!`;
    io:println(x2);

    // An XML comment.
    xml x3 = xml `<!--I am a comment-->`;
    io:println(x3);

    // An XML processing instructions.
    xml x4 = xml `<?target data?>`;
    io:println(x4);

    // Multiple XML items can be combined to form a sequence of XML.
    // The resulting sequence is another XML on its own.
    xml x5 = x1 + x2 + x3 + x4;
    io:println(x5);

}

XML Data Model

An xml value is a sequence representing the parsed content of an XML element.

An xml value has four kinds of items.

  • element, processing instruction and comment item correspond 1:1 to XML infoset items
  • text item corresponds to one or more Character Information Items


XML document is an xml sequence with only one element and no text. An element item is mutable and consists of:

  • name: type string
  • attributes: type map<string>
  • children: type xml


A text item is immutable.

  • it has no identity: == is the same as ===
  • consecutive text items never occur in an xml value: they are always merged

import ballerina/io;
public function main() {
    xml x1 = xml `<book>The Lost World</book>`;
    io:println(x1);

An XML element. There can be only one root element.

    xml x2 = xml `Hello, world!`;
    io:println(x2);

An XML text.

    xml x3 = xml `<!--I am a comment-->`;
    io:println(x3);

An XML comment.

    xml x4 = xml `<?target data?>`;
    io:println(x4);

An XML processing instructions.

    xml x5 = x1 + x2 + x3 + x4;
    io:println(x5);

Multiple XML items can be combined to form a sequence of XML. The resulting sequence is another XML on its own.

}
bal run xml.bal
<book>The Lost World</book>
Hello, world!
<!--I am a comment-->
<?target data?>
<book>The Lost World</book>Hello, world!<!--I am a comment--><?target data?>
import ballerina/io;

public function main() {
    // An XML element. There can be only one root element.
    xml x1 = xml `<book>The Lost World</book>`;
    io:println(x1);

    // An XML text.
    xml x2 = xml `Hello, world!`;
    io:println(x2);

    // An XML comment.
    xml x3 = xml `<!--I am a comment-->`;
    io:println(x3);

    // An XML processing instructions.
    xml x4 = xml `<?target data?>`;
    io:println(x4);

    // Multiple XML items can be combined to form a sequence of XML.
    // The resulting sequence is another XML on its own.
    xml x5 = x1 + x2 + x3 + x4;
    io:println(x5);

}

XML Data Model

An xml value is a sequence representing the parsed content of an XML element.

An xml value has four kinds of items.

  • element, processing instruction and comment item correspond 1:1 to XML infoset items
  • text item corresponds to one or more Character Information Items


XML document is an xml sequence with only one element and no text. An element item is mutable and consists of:

  • name: type string
  • attributes: type map<string>
  • children: type xml


A text item is immutable.

  • it has no identity: == is the same as ===
  • consecutive text items never occur in an xml value: they are always merged

import ballerina/io;
public function main() {
    xml x1 = xml `<book>The Lost World</book>`;
    io:println(x1);

An XML element. There can be only one root element.

    xml x2 = xml `Hello, world!`;
    io:println(x2);

An XML text.

    xml x3 = xml `<!--I am a comment-->`;
    io:println(x3);

An XML comment.

    xml x4 = xml `<?target data?>`;
    io:println(x4);

An XML processing instructions.

    xml x5 = x1 + x2 + x3 + x4;
    io:println(x5);

Multiple XML items can be combined to form a sequence of XML. The resulting sequence is another XML on its own.

}
bal run xml.bal
<book>The Lost World</book>
Hello, world!
<!--I am a comment-->
<?target data?>
<book>The Lost World</book>Hello, world!<!--I am a comment--><?target data?>

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand