%% Add Comment to Document
% This example adds a comment to the root node of an XML document that represents the days of the week.
% Create a matlab.io.xml.dom.Document object with a root element named weekdays.

import matlab.io.xml.dom.*
doc = Document("weekdays");
docRootNode = getDocumentElement(doc);

% Use the createComment method of the Document object to create a comment. Append the comment to the root element.

appendChild(docRootNode,createComment(doc,"days of the week except Saturday and Sunday"));

% For each week day, Monday through Friday, create an element named day and append the name of the day to the day element. Append the day elements to the root element.

weekdays = ["Mon" "Tue" "Wed" "Thu" "Fri"];
for i=1:5
    dayElement = createElement(doc,"day");
    appendChild(dayElement,createTextNode(doc,weekdays(i)));
    appendChild(docRootNode,dayElement);
end

% Write the document to the file weekdays.xml;

xmlFileName = "weekdays.xml";
writer = matlab.io.xml.dom.DOMWriter;
writer.Configuration.FormatPrettyPrint = true;
writeToFile(writer,doc,xmlFileName);

% Display the file contents.

type weekdays.xml;

% The comment <!--days of the week except Saturday and Sunday--> immediately follows the opening tag of the root element weekdays.
%
% Copyright 2020 The MathWorks, Inc.