Main Content

matlab.io.xml.dom.DocumentFragment Class

Namespace: matlab.io.xml.dom

Group of document nodes

Since R2021a

Description

Use an object of the matlab.io.xml.dom.DocumentFragment class as a container for a group of document nodes. Appending a document fragment to another node appends the children of the fragment but not the fragment itself. Similarly, inserting a fragment inserts the children but not the fragment. A fragment does not have to be well-formed XML. For example, a fragment can contain multiple top-level nodes or a single text node.

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

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

You can create a matlab.io.xml.dom.DocumentFragment object by using the createDocumentFragment method of a matlab.io.xml.dom.Document object.

Properties

expand all

Text content of this document fragment, specified as a character vector or string scalar. This property contains the concatenated textual content of the children of this fragment.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Methods

expand all

Examples

collapse all

Suppose that your application creates chapters and that the number of chapters is determined at run time. You can write a function, such as the createChapters function, defined at the end of this example, to create a specified number of chapter elements and return them as a document fragment.

Create a document that has a root element named book.

import matlab.io.xml.dom.*

doc = Document("book");
docElemRoot = getDocumentElement(doc);

Call the function createChapters to return three chapters as a matlab.io.xml.dom.DocumentFragment object. Append the fragment to the document.

docFrag = createChapters(doc,3);
appendChild(docElemRoot,docFrag);

Write the document to the file book.xml.

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

Display the resulting 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>

The createChapters Function

The function createChapters returns a DocumentFragment object that contains the specified number of chapter elements.

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

Version History

Introduced in R2021a