Main Content

matlab.io.xml.dom.Comment Class

Namespace: matlab.io.xml.dom

Comment in XML document

Since R2021a

Description

An object of the mlreportgen.io.xml.dom.Comment class represents a comment in an XML document.

The matlab.io.xml.dom.Comment class is a handle class.

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Create a matlab.io.xml.dom.Comment object by using the createComment method of a matlab.io.xml.dom.Document object.

Properties

expand all

Number of characters in the comment, specified as a double.

Attributes:

GetAccess
public
SetAccess
immutable
Transient
true
NonCopyable
true

Text content of the comment, specified as a character vector.

Attributes:

GetAccess
public
SetAccess
immutable
Transient
true
NonCopyable
true

Methods

expand all

Examples

collapse all

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;
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<weekdays>
  <!--days of the week except Saturday and Sunday-->
  <day>Mon</day>
  <day>Tue</day>
  <day>Wed</day>
  <day>Thu</day>
  <day>Fri</day>
</weekdays>

The comment <!--days of the week except Saturday and Sunday--> immediately follows the opening tag of the root element weekdays.

Version History

Introduced in R2021a