%% Create an Attribute and Add It to an Element
% This example creates an attribute using the createAttribute method of the owner document and adds the attribute to an element using the setAttributeNode method of the element.
% Import the matlab.io.xml.dom package so that you do not have to use long, fully qualified class names.

import matlab.io.xml.dom.*

% Create a document with the root element para.

doc = Document("para");
para = getDocumentElement(doc);

% Create an attribute Color and set the attribute value.

attrObj = createAttribute(doc,"Color");
setValue(attrObj,"red");

% Add the Color attribute to the para element.

setAttributeNode(para,attrObj);

% Create a text node and add it to the para element.

textNode = createTextNode(doc,"Hello");
appendChild(para,textNode);

% Write the XML to a file.

xmlFileName = "para.xml";
writer = matlab.io.xml.dom.DOMWriter;
writeToFile(writer,doc,xmlFileName);

%
% Copyright 2020 The MathWorks, Inc.