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
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
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
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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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)