matlab.io.xml.dom.Attr 类
包: matlab.io.xml.dom
创建对象
使用以下方法之一创建 matlab.io.xml.dom.Attr
对象:
使用
matlab.io.xml.dom.Document
对象的createAttribute
或createAttributeNS
方法创建Attr
对象。使用Element
对象的setAttributeNode
或setAttributeNodeNS
方法,将Attr
对象添加到matlab.io.xml.dom.Element
对象。使用
Element
对象的setAttribute
或setAttributeNS
方法,创建Attr
对象并将其添加到matlab.io.xml.dom.Element
对象。
属性
IsID
— 属性是否为 ID 属性
false
(默认) | true
此属性是否为 ID 属性,指定为 true
或 false
。
如果元素的 ID 属性具有唯一值,您可以使用文档中的 getElementByID
方法来访问该元素。
属性:
GetAccess | public |
SetAccess | immutable |
Transient | true |
NonCopyable | true |
Name
— 属性名称
字符向量
此属性的名称,指定为字符向量。
属性:
GetAccess | public |
SetAccess | immutable |
Transient | true |
NonCopyable | true |
Value
— 属性值
字符向量 | 字符串标量
此属性的值,指定为字符向量或字符串标量。
属性:
GetAccess | public |
SetAccess | public |
NonCopyable | true |
方法
公共方法
cloneNode |
|
compareDocumentPosition |
例如: import matlab.io.xml.dom.* d = Document('root'); root = getDocumentElement(d); setAttribute(root,"Color","red"); a = getAttributeNode(root,"Color"); pos = compareDocumentPosition(a,root); if bitor(pos,a.DOCUMENT_POSITION_CONTAINS) == pos disp("root contains color attribute"); else disp("root does not contain color attribute"); end |
getBaseURI |
属性节点的基本 URI 是拥有该属性节点的文档的 URI。 |
getLength |
|
getLocalName |
|
getNextSibling |
|
getName | name = getName(thisAttr) 以字符向量形式返回此属性的名称。例如,如果此属性的名称是 'color' ,则该方法返回 'color' 。 |
getNamespaceURI |
|
getNodeName | name = getNodeName(thisAttr) 以字符向量形式返回此属性的名称。例如,如果此属性的名称是 'color' ,则该方法返回 'color' 。 |
getNodeType |
此方法提供与基于 W3C XML DOM 标准的现有 MATLAB® 代码的兼容性。 对于新的 MATLAB 代码,请使用 import matlab.io.xml.dom.* d = Document('root'); root = getDocumentElement(d); setAttribute(root,"Color","red"); attrNode = getAttributeNode(root,"Color") if isa(attrNode,'matlab.io.xml.dom.Attr') fprintf('This is an attribute node.\n'); end |
getNodeTypeName | name = getNodeTypeName(thisAttr) 返回 'ATTRIBUTE_NODE' 。 |
getNodeValue |
|
getOwnerElement |
|
getOwnerDocument |
|
getParentNode | parent = getParentNode(thisAttr) 返回空节点,因为属性不是子级。 |
getPrefix |
|
getPreviousSibling |
|
getSchemaTypeInfo |
|
getSpecified | 如果此属性值是在解析此属性的 XML 标记中指定的,或是以编程方式设置的,则 |
getTextContent | getTextContent(thisAttr) 以字符向量形式返回此属性的值。 |
getValue | getValue(thisAttr) 以字符向量形式返回此属性的值。 |
isEqualNode | 如果 此方法测试节点的相等性,而不测试节点是否为同一对象的句柄。要测试相同性,请使用 相同的节点也会相等,但相等的节点未必相同。 在测试文档的相等性之前请先对它们进行标准化,因为标准化会影响相等性。 |
isID | 如果此属性是 ID 属性,则 如果元素的 ID 属性具有唯一值,您可以使用文档的 |
isSameNode | 如果 |
lookupNamespaceURI |
|
lookupPrefix |
|
setNodeValue |
以字符向量或字符串标量形式指定 |
setTextContent |
以字符向量或字符串标量形式指定 |
setValue |
以字符向量或字符串标量形式指定 |
示例
向元素添加属性
此示例通过使用 Element
对象的 setAttribute
方法,创建一个 matlab.io.xml.dom.Attr
对象并将其添加到 matlab.io.xml.dom.Element
对象。
导入 matlab.io.xml.dom
包,这样就不必使用冗长的完全限定类名称。
import matlab.io.xml.dom.*
使用根元素 para
创建文档。
doc = Document("para");
para = getDocumentElement(doc);
向 para
元素添加 Color
属性。
setAttribute(para,"Color","red");
向 para
元素添加文本。
textNode = createTextNode(doc,"Hello");
appendChild(para,textNode);
将 XML 写入文件。
xmlFileName = "para.xml";
writer = matlab.io.xml.dom.DOMWriter;
writeToFile(writer,doc,xmlFileName);
创建属性并将其添加到元素
此示例使用所有者文档的 createAttribute
方法创建一个属性,并使用元素的 setAttributeNode
方法将该属性添加到元素。
导入 matlab.io.xml.dom
包,这样就不必使用冗长的完全限定类名称。
import matlab.io.xml.dom.*
使用根元素 para
创建文档。
doc = Document("para");
para = getDocumentElement(doc);
创建属性 Color
并设置属性值。
attrObj = createAttribute(doc,"Color"); setValue(attrObj,"red");
将 Color
属性添加到 para
元素。
setAttributeNode(para,attrObj);
创建一个文本节点,并将其添加到 para
元素。
textNode = createTextNode(doc,"Hello");
appendChild(para,textNode);
将 XML 写入文件。
xmlFileName = "para.xml";
writer = matlab.io.xml.dom.DOMWriter;
writeToFile(writer,doc,xmlFileName);
版本历史记录
在 R2021a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)