Main Content


Write XML Document Object Model node



xmlwrite(filename,DOMnode) writes the Document Object Model (DOM) node DOMnode to the file filename.

Working with xmlwrite requires that you use the Java® API for XML Processing (JAXP). For more information, see


chr = xmlwrite(DOMnode) returns a serialized DOM node as a character vector.


collapse all

Write an XML file by, first, creating a Document Object Model (DOM) node containing the XML data. Then, write the DOM node to an XML file. The final XML file should contain this text.

<?xml version="1.0" encoding="utf-8"?>

<toc version="2.0">

<tocitem target="upslope_product_page.html">Upslope Area Toolbox<!-- Functions -->

<tocitem target="demFlow_help.html">demFlow</tocitem>

<tocitem target="facetFlow_help.html">facetFlow</tocitem>

<tocitem target="flowMatrix_help.html">flowMatrix</tocitem>

<tocitem target="pixelFlow_help.html">pixelFlow</tocitem>



First, create the DOM node object and root element, and populate the elements and the attributes of the node corresponding to the XML data.

docNode = com.mathworks.xml.XMLUtils.createDocument('toc');

Identify the root element, and set the version attribute.

toc = docNode.getDocumentElement;

Add the tocitem element node for the product page. Each tocitem element in this file has a target attribute and a child text node.

product = docNode.createElement('tocitem');
product.appendChild(docNode.createTextNode('Upslope Area Toolbox'));

Add comment.

product.appendChild(docNode.createComment(' Functions '));

Add a tocitem element node for each function, where the target is of the form function_help.html.

functions = {'demFlow','facetFlow','flowMatrix','pixelFlow'};
for idx = 1:numel(functions)
    curr_node = docNode.createElement('tocitem');
    curr_file = [functions{idx} '_help.html']; 
    % Child text is the function name.

Finally, export the DOM node to an XML file named infoUAT.xml, and view the file using the type function.


Read a DOM node from a sample XML file and get the contents of the DOM node as a character vector.

Display the contents of the sample XML file, and then import the DOM node from the file.

sampleXMLfile = 'sample.xml';
DOMnode = xmlread(sampleXMLfile);

Use xmlwrite to return the DOMnode object as a serialized character vector.

text = xmlwrite(DOMnode)
text = 
    '<?xml version="1.0" encoding="utf-8"?>
        <name>Example Manager</name>
              <label>Example Manager</label>

Input Arguments

collapse all

File name, specified as a character vector or string scalar containing the name of the local file or URL.

Data Types: char | string

Document Object Model(DOM) node, specified as a DOM node object.

Document Object Model is defined by the World Wide Web consortium. For more information, see The XML Document Object Model.

Version History

Introduced before R2006a