Main Content

matlab.io.xml.dom.Attr class

Package: matlab.io.xml.dom

Attribute of XML element

Description

A matlab.io.xml.dom.Attr object represents an attribute of an XML element.

The matlab.io.xml.dom.Attr 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.Attr object by using one of these approaches:

  • Create an Attr object by using the createAttribute or createAttributeNS methods of a matlab.io.xml.dom.Document object. Add the Attr object to a matlab.io.xml.dom.Element object by using the setAttributeNode or setAttributeNodeNS methods of the Element object.

  • Create and add an Attr object to a matlab.io.xml.dom.Element object by using the setAttribute or setAttributeNS methods of the Element object.

Properties

expand all

Whether this attribute is an ID attribute, specified as true or false.

If an element has an ID attribute with a unique value, you can use the getElementByID method of the document to access the element.

Attributes:

GetAccess
public
SetAccess
immutable
Transient
true
NonCopyable
true

Name of this attribute, specified as a character vector.

Attributes:

GetAccess
public
SetAccess
immutable
Transient
true
NonCopyable
true

Value of this attribute, specified as a character vector or string scalar.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Methods

expand all

Examples

collapse all

This example creates and adds a matlab.io.xml.dom.Attr object to a matlab.io.xml.dom.Element object by using the setAttribute method of the Element object.

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);

Add a Color attribute to the para element.

setAttribute(para,"Color","red");

Add text 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);

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);
Introduced in R2021a