matlab.io.xml.dom.DocumentFragment Class
Namespace: matlab.io.xml.dom
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
TextContent
— Text content of document fragment
''
(default) | character vector | string scalar
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
Public Methods
appendChild |
|
child |
|
cloneNode |
|
compareDocumentPosition |
|
getAttributes |
|
getChildNodes |
|
getChildren |
|
getFirstChild | child = getFirstChild(thisFragment) returns the first child
of this fragment. |
getLastChild | child = getLastChild(thisFragment) returns the last child of
this fragment. |
getLength |
|
getLocalName |
|
getNamespaceURI |
|
getNextSibling |
|
getNodeName |
|
getNodeType |
This method provides compatibility with existing MATLAB® code that is based on the W3C XML DOM standard. For new
MATLAB code, use 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) returns an empty node
object because document fragments do not have parents. |
getPrefix |
|
getPreviousSibling |
|
getTextContent | getTextContent(thisFragment) returns the text content of
this fragment. If this fragment has children, the method returns the concatenated
text content of the children. |
hasAttributes |
|
hasChildNodes |
|
insertBefore |
If
|
isDefaultNamespace |
|
isEqualNode |
This method tests for equality of nodes, not whether the nodes
are handles to the same object. To test for sameness, use the
Nodes that are the same are also equal, but nodes that are equal are not necessarily the same. |
isSameNode |
|
item |
|
lookupNamespaceURI |
|
lookupPrefix |
|
normalize |
|
removeChild |
|
replaceChild |
|
setNodeValue |
|
setPrefix |
|
setTextContent |
Specify |
Examples
Use Document Fragment to Create Content Based on Run-Time Data
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
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.
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)