matlab.io.xml.dom.DocumentFragment 类
命名空间: matlab.io.xml.dom
描述
创建对象
您可以使用 matlab.io.xml.dom.Document
对象的 createDocumentFragment
方法创建 matlab.io.xml.dom.DocumentFragment
对象。
属性
此文档片段的文本内容,指定为字符向量或字符串标量。此属性包含此片段的子级的串联文本内容。
属性:
GetAccess | public |
SetAccess | public |
NonCopyable | true |
方法
appendChild |
|
child |
|
cloneNode |
|
compareDocumentPosition |
|
getAttributes |
|
getChildNodes |
|
getChildren |
|
getFirstChild | child = getFirstChild(thisFragment) 返回此片段的第一个子级。 |
getLastChild | child = getLastChild(thisFragment) 返回此片段的最后一个子级。 |
getLength |
|
getLocalName |
|
getNamespaceURI |
|
getNextSibling |
|
getNodeName |
|
getNodeType |
此方法提供与基于 W3C XML DOM 标准的现有 MATLAB® 代码的兼容性。 对于新的 MATLAB 代码,请使用 import matlab.io.xml.dom.* d = Document('book'); frag= createDocumentFragment(d); if isa(frag,'matlab.io.xml.dom.DocumentFragment') fprintf('This is an document fragment.\n'); end |
getNodeValue |
|
getOwnerDocument |
|
getParentNode | parent = getParentNode(thisFragment) 返回空节点对象,因为文档片段没有父级。 |
getPrefix |
|
getPreviousSibling |
|
getTextContent | getTextContent(thisFragment) 返回此片段的文本内容。如果此片段有子级,则该方法返回子级的串联文本内容。 |
hasAttributes |
|
hasChildNodes | 如果此片段有子级,则 |
insertBefore |
如果 |
isDefaultNamespace |
|
isEqualNode | 如果 此方法测试节点的相等性,而不测试节点是否为同一对象的句柄。要测试相同性,请使用 相同的节点也会相等,但相等的节点未必相同。 |
isSameNode | 如果 |
item |
|
lookupNamespaceURI |
|
lookupPrefix |
|
normalize |
|
removeChild |
|
replaceChild |
|
setNodeValue |
|
setPrefix |
|
setTextContent |
以字符向量或字符串标量形式指定 |
示例
假设您的应用程序创建章节,并且章节的数量是在运行时确定的。您可以编写一个函数(如此示例末尾定义的 createChapters
函数)来创建指定数量的章节元素,并以文档片段形式返回它们。
创建一个具有名为 book
的根元素的文档。
import matlab.io.xml.dom.* doc = Document("book"); docElemRoot = getDocumentElement(doc);
调用函数 createChapters
以 matlab.io.xml.dom.DocumentFragment
对象形式返回三个章节。将该片段追加到文档中。
docFrag = createChapters(doc,3); appendChild(docElemRoot,docFrag);
将文档写入文件 book.xml
。
xmlFileName = "book.xml";
writer = matlab.io.xml.dom.DOMWriter;
writeToFile(writer,doc,xmlFileName);
显示生成的 XML。
type(xmlFileName);
<?xml version="1.0" encoding="UTF-8" standalone="no" ?><book><chapter>Chapter 1</chapter><chapter>Chapter 2</chapter><chapter>Chapter 3</chapter></book>
createChapters
函数
函数 createChapters
返回 DocumentFragment
对象,该对象包含指定数量的章节元素。
function docFrag = createChapters(doc,n) docFrag = createDocumentFragment(doc); for i=1:n chapter = createElement(doc,"chapter"); appendChild(chapter,createTextNode(doc,sprintf("Chapter %d",i))); appendChild(docFrag,chapter); end end
版本历史记录
在 R2021a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)