Swan Lake Beta3 is here.
import ballerina/io;

public function main() returns error? {
    xml x1 = xml `<name>Sherlock Holmes</name>`;
    xml:Element x2 = 
        xml `<details>
                <author>Sir Arthur Conan Doyle</author>
                <language>English</language>
            </details>`;

    // `+` does concatenation.
    xml x3 = x1 + x2;

    io:println(x3);

    xml x4 = xml `<name>Sherlock Holmes</name><details>
                        <author>Sir Arthur Conan Doyle</author>
                        <language>English</language>
                  </details>`;
    // `==` does deep equals.
    boolean eq = x3 == x4;

    io:println(eq);

    // `foreach` iterates over each item.
    foreach var item in x4 {
        io:println(item);
    }

    // `x[i]` gives i-th item (empty sequence if none).
    io:println(x3[0]);

    // `x.id` accesses required attribute named `id`:
    // result is `error` if there is no such attribute
    // or if `x` is not a singleton.
    xml x5 = xml `<para id="greeting">Hello</para>`;
    string id = check x5.id;

    io:println(id);

    // `x?.id` accesses optional attribute named `id`:
    // result is `()` if there is no such attribute.
    string? name = check x5?.name;

    io:println(name is ());

    // Mutate an element using `e.setChildren(x)`.
    x2.setChildren(xml `<language>French</language>`);

    io:println(x2);
    io:println(x3);
}

XML Operations

+ does concatenation. == does deep equals. foreach iterates over each item. x[i] gives i-th item (empty sequence if none). x.id accesses required attribute named id: result is error if there is no such attribute or if x is not a singleton. x?.id accesses optional attribute named id: result is () if there is no such attribute. Langlib lang.xml provides other operations. Mutate an element using e.setChildren(x).

import ballerina/io;
public function main() returns error? {
    xml x1 = xml `<name>Sherlock Holmes</name>`;
    xml:Element x2 = 
        xml `<details>
                <author>Sir Arthur Conan Doyle</author>
                <language>English</language>
            </details>`;
    xml x3 = x1 + x2;

+ does concatenation.

    io:println(x3);
    xml x4 = xml `<name>Sherlock Holmes</name><details>
                        <author>Sir Arthur Conan Doyle</author>
                        <language>English</language>
                  </details>`;
    boolean eq = x3 == x4;

== does deep equals.

    io:println(eq);
    foreach var item in x4 {
        io:println(item);
    }

foreach iterates over each item.

    io:println(x3[0]);

x[i] gives i-th item (empty sequence if none).

    xml x5 = xml `<para id="greeting">Hello</para>`;
    string id = check x5.id;

x.id accesses required attribute named id: result is error if there is no such attribute or if x is not a singleton.

    io:println(id);
    string? name = check x5?.name;

x?.id accesses optional attribute named id: result is () if there is no such attribute.

    io:println(name is ());
    x2.setChildren(xml `<language>French</language>`);

Mutate an element using e.setChildren(x).

    io:println(x2);
    io:println(x3);
}
bal run xml_operations.bal
<name>Sherlock Holmes</name><details>
                <author>Sir Arthur Conan Doyle</author>
                <language>English</language>
            </details>
false
<name>Sherlock Holmes</name>
<details>
                        <author>Sir Arthur Conan Doyle</author>
                        <language>English</language>
                  </details>
<name>Sherlock Holmes</name>
greeting
true
<details><language>French</language></details>
<name>Sherlock Holmes</name><details><language>French</language></details>
import ballerina/io;

public function main() returns error? {
    xml x1 = xml `<name>Sherlock Holmes</name>`;
    xml:Element x2 = 
        xml `<details>
                <author>Sir Arthur Conan Doyle</author>
                <language>English</language>
            </details>`;

    // `+` does concatenation.
    xml x3 = x1 + x2;

    io:println(x3);

    xml x4 = xml `<name>Sherlock Holmes</name><details>
                        <author>Sir Arthur Conan Doyle</author>
                        <language>English</language>
                  </details>`;
    // `==` does deep equals.
    boolean eq = x3 == x4;

    io:println(eq);

    // `foreach` iterates over each item.
    foreach var item in x4 {
        io:println(item);
    }

    // `x[i]` gives i-th item (empty sequence if none).
    io:println(x3[0]);

    // `x.id` accesses required attribute named `id`:
    // result is `error` if there is no such attribute
    // or if `x` is not a singleton.
    xml x5 = xml `<para id="greeting">Hello</para>`;
    string id = check x5.id;

    io:println(id);

    // `x?.id` accesses optional attribute named `id`:
    // result is `()` if there is no such attribute.
    string? name = check x5?.name;

    io:println(name is ());

    // Mutate an element using `e.setChildren(x)`.
    x2.setChildren(xml `<language>French</language>`);

    io:println(x2);
    io:println(x3);
}

XML Operations

+ does concatenation. == does deep equals. foreach iterates over each item. x[i] gives i-th item (empty sequence if none). x.id accesses required attribute named id: result is error if there is no such attribute or if x is not a singleton. x?.id accesses optional attribute named id: result is () if there is no such attribute. Langlib lang.xml provides other operations. Mutate an element using e.setChildren(x).

import ballerina/io;
public function main() returns error? {
    xml x1 = xml `<name>Sherlock Holmes</name>`;
    xml:Element x2 = 
        xml `<details>
                <author>Sir Arthur Conan Doyle</author>
                <language>English</language>
            </details>`;
    xml x3 = x1 + x2;

+ does concatenation.

    io:println(x3);
    xml x4 = xml `<name>Sherlock Holmes</name><details>
                        <author>Sir Arthur Conan Doyle</author>
                        <language>English</language>
                  </details>`;
    boolean eq = x3 == x4;

== does deep equals.

    io:println(eq);
    foreach var item in x4 {
        io:println(item);
    }

foreach iterates over each item.

    io:println(x3[0]);

x[i] gives i-th item (empty sequence if none).

    xml x5 = xml `<para id="greeting">Hello</para>`;
    string id = check x5.id;

x.id accesses required attribute named id: result is error if there is no such attribute or if x is not a singleton.

    io:println(id);
    string? name = check x5?.name;

x?.id accesses optional attribute named id: result is () if there is no such attribute.

    io:println(name is ());
    x2.setChildren(xml `<language>French</language>`);

Mutate an element using e.setChildren(x).

    io:println(x2);
    io:println(x3);
}
bal run xml_operations.bal
<name>Sherlock Holmes</name><details>
                <author>Sir Arthur Conan Doyle</author>
                <language>English</language>
            </details>
false
<name>Sherlock Holmes</name>
<details>
                        <author>Sir Arthur Conan Doyle</author>
                        <language>English</language>
                  </details>
<name>Sherlock Holmes</name>
greeting
true
<details><language>French</language></details>
<name>Sherlock Holmes</name><details><language>French</language></details>
Subscribe to the newsletter

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