import ballerina/io;

public function main() returns error? {
    // Initializes the XML file path and content.
    string xmlFilePath = "./files/xmlFile.xml";
    xml xmlContent = xml `<book>The Lost World</book>`;

    // Writes the given XML to a file.
    check io:fileWriteXml(xmlFilePath, xmlContent);
    // If the write operation was successful, then, performs a read operation to read the XML content.
    xml readXml = check io:fileReadXml(xmlFilePath);
    io:println(readXml);
}

Read/Write XML

This example demonstrates how XML content can be read from a file and written to a file using a character channel and the readXml() and writeXml() functions of the I/O API.

For more information on the underlying module, see the IO module.

import ballerina/io;
public function main() returns error? {
    string xmlFilePath = "./files/xmlFile.xml";
    xml xmlContent = xml `<book>The Lost World</book>`;

Initializes the XML file path and content.

    check io:fileWriteXml(xmlFilePath, xmlContent);

Writes the given XML to a file.

    xml readXml = check io:fileReadXml(xmlFilePath);
    io:println(readXml);
}

If the write operation was successful, then, performs a read operation to read the XML content.

bal run io_xml.bal
<book>The Lost World</book>