import ballerina/io;public function main() {
    xml x1 = xml `<book>The Lost World</book>`;
    io:println(x1);
    xml x2 = xml `Hello, world!`;
    io:println(x2);
    xml x3 = xml `<!--I am a comment-->`;
    io:println(x3);
    xml x4 = xml `<?target data?>`;
    io:println(x4);
    xml x5 = x1 + x2 + x3 + x4;
    io:println("\nResulting XML sequence:");
    io:println(x5);
}

XML

The XML type in Ballerina represents a sequence of zero or more XML items. Each item can be an element, a text, a comment, or a processing instruction.

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

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

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

The XML text.

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

The XML comment.

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

The XML processing instruction.

    xml x5 = x1 + x2 + x3 + x4;
    io:println("\nResulting XML sequence:");
    io:println(x5);
}

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

# To run this sample, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command.
$ ballerina run xml.bal
<book>The Lost World</book>
Hello, world!
<!--I am a comment-->
<?target data?>
Resulting XML sequence:
<book>The Lost World</book>Hello, world!<!--I am a comment--><?target data?>