import ballerina/io;function main (string[] args) {
    xml bookName = xml `<name>Book1</name>`;
    xml bookComment = xml `<!--some comment-->`;
    xml someText = xml `Hello, World!`;
    xml content = someText + bookName + bookComment;
    string xmlString = "<book/>";
    xml book = <xml>xmlString;
    io:println(bookComment.getItemType());
    io:println(bookName.getElementName());
    io:println(bookName.getTextValue());
    io:println(content.isEmpty());
    io:println(content.isSingleton());
    xml x = content.slice(2, 3);
    io:println(x);
    x = content.elements();
    io:println(x);
    x = content.select("name");
    io:println(x);
    book.setChildren(content);
    io:println(book);
    x = book.*;
    io:println(x);
    x = book.selectChildren("name");
    io:println(x);
    x = content.strip();
    io:println(x);
    x = bookComment.copy();
    io:println(x);
}

XML Functions

Ballerina supports various built-in functions to manipulate XML.

import ballerina/io;
function main (string[] args) {
    xml bookName = xml `<name>Book1</name>`;
    xml bookComment = xml `<!--some comment-->`;
    xml someText = xml `Hello, World!`;
    xml content = someText + bookName + bookComment;
    string xmlString = "<book/>";
    xml book = <xml>xmlString;

Other than the XML literal syntax, XML can be parsed using a string. The string should be a valid XML.

    io:println(bookComment.getItemType());

Get the type of the XML element.

    io:println(bookName.getElementName());

Get the name of an XML element.

    io:println(bookName.getTextValue());

Get the text content of an XML element.

    io:println(content.isEmpty());

Check if the XML element is emtpy.

    io:println(content.isSingleton());

Check if the XML element has only one value.

    xml x = content.slice(2, 3);
    io:println(x);

Get a subsequence of an XML sequence.

    x = content.elements();
    io:println(x);

All the element-type items are taken from an XML sequence.

    x = content.select("name");
    io:println(x);

An XML element with a particular name can be retrieved.

    book.setChildren(content);
    io:println(book);

Set the children of an XML element.

    x = book.*;
    io:println(x);

Get all the children of an XML element.

    x = book.selectChildren("name");
    io:println(x);

Get a particular child of an XML element.

    x = content.strip();
    io:println(x);

Remove any text items from an XML sequence that are all whitespace.

    x = bookComment.copy();
    io:println(x);
}

Make a copy of an XML element.

$ ballerina run xml-functions.bal
comment

Type of the XML element.

name

Name of the XML element.

Book1

Text content of the XML element.

false

Is the XML emtpy?

false

Does it have only one element?

<!--some comment-->

Subsequence of a sequence.

<name>Book1</name>

All the element type items.

<name>Book1</name>

Selected element.

<book>Hello, World!<name>Book1</name><!--some comment--></book>

XML after setting the children.

Hello, World!<name>Book1</name><!--some comment-->

All the children.

<name>Book1</name>

Selected child.

Hello, World!<name>Book1</name><!--some comment-->

Stripped XML.

<!--some comment-->

Copied XML.