Main Content

importNode

Class: matlab.io.xml.dom.Document
Namespace: matlab.io.xml.dom

Import node from another document into this document

Since R2021a

Syntax

node = importNode(thisDoc,node,deep)
node = importNode(thisDoc,node)

Description

node = importNode(thisDoc,node,deep) imports a node from another document into this document. This method creates a copy of the node, assigns ownership of the copy to this document, and returns the copy. The imported node has no parent. Use the appendChild method of this document to insert the imported node into the document tree.

Imported nodes have the same node name, node type, namespace URI, prefix, and local name as the source node. This table describes additional information that is copied, depending on the node type.

Node TypeAdditional Imported Information
matlab.io.xml.dom.Attr

In the copy, the ownerElement attribute is set to empty and the specified flag is set to true. The descendants of the source Attr node are recursively imported and the resulting nodes are reassembled to form the corresponding subtree. The deep argument has no effect on Attr nodes because the children are always copied.

matlab.io.xml.dom.DocumentFragment

For a deep copy, the descendants of the source element are recursively imported and the resulting nodes are reassembled to form the corresponding subtree. Otherwise, the copy is an empty DocumentFragment.

matlab.io.xml.dom.Document, matlab.io.xml.dom.DocumentTypeYou cannot import these types of nodes.
matlab.io.xml.dom.Element

Specified attribute nodes of the source element are imported, and the resulting Attr nodes are attached to the copy of the Elementnode. Default attributes are not copied. However, if the importing document defines default attributes for the element being imported, those attributes are assigned to the element. If the deep argument is true, the descendants of the source element are recursively imported and the resulting nodes are reassembled to form the corresponding subtree.

matlab.io.xml.dom.Entity

publicId, systemId, and notationName attributes are copied.

You cannot add imported Entity nodes to the DocumentType node of the document because the DocumentType is read-only.

matlab.io.xml.dom.EntityReference

Only the EntityReference node itself is copied, even if you specify a deep copy, because the entity can be defined differently in the source and destination documents. If the source document provides a definition for the referenced entity, the value is assigned to the imported entity reference.

matlab.io.xml.dom.Notation

publicId and systemId attributes are copied.

You cannot add imported Notation nodes to the document DocumentType node because the DocumentType node is read-only.

The deep argument has no effect on Notation nodes because they do not have children.

matlab.io.xml.dom.ProcessingInstructionTarget and data values are copied.
matlab.io.xml.dom.Text, matlab.io.xml.dom.CDATASection, and matlab.io.xml.dom.Comment

Data and length attributes are copied.

node = importNode(thisDoc,node) imports a deep copy of the specified node.

Input Arguments

expand all

Document to import node to, specified as a matlab.io.xml.dom.Document object.

Whether copy is deep, specified as true or false. If the value is true, the imported copy of the node is a deep copy. Otherwise, it is a shallow copy.

Examples

expand all

Create a node for a weekdays element in one document and import the node into a second document.

Create a document that has a weekdays element.

import matlab.io.xml.dom.*
doc1 = Document("root_element");
doc1RootNode = getDocumentElement(doc1);
weekdays = ["Mon" "Tue" "Wed" "Thu" "Fri"];
weekdaysElement = createElement(doc1,"weekdays");
for i=1:5
    dayElement = createElement(doc1,"day");
    appendChild(dayElement,createTextNode(doc1,weekdays(i)));
    appendChild(weekdaysElement,dayElement);
end
appendChild(doc1RootNode,weekdaysElement);

Create a second document.

doc2 = Document("root_element2");
doc2RootNode = getDocumentElement(doc2);

Import the weekdays element into the second document and append the imported node to the root node.

importedNode = importNode(doc2,weekdaysElement);
appendChild(doc2RootNode,importedNode);

Write the document to a file.

xmlFileName = "weekdays.xml";
writer = matlab.io.xml.dom.DOMWriter;
writeToFile(writer,doc2,xmlFileName);

Version History

Introduced in R2021a