Exporting to XML Documents

To write data to an XML file, use the xmlwrite function. xmlwrite requires that you describe the file in a Document Object Model (DOM) node. For an introduction to DOM nodes, see What Is an XML Document Object Model (DOM)?

For more information, see:

Creating an XML File

Although each file is different, these are common steps for creating an XML document:

  1. Create a document node and define the root element by calling this method:

    docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
  2. Get the node corresponding to the root element by calling getDocumentElement. The root element node is required for adding child nodes.

  3. Add element, text, comment, and attribute nodes by calling methods on the document node. Useful methods include:

    • createElement

    • createTextNode

    • createComment

    • setAttribute

    For a complete list of the methods and properties of DOM nodes, see the org.w3c.dom package description at http://download.oracle.com/javase/6/docs/api/.

  4. As needed, define parent/child relationships by calling appendChild on the parent node.

      Tip   Text nodes are always children of element nodes. To add a text node, call createTextNode on the document node, and then call appendChild on the parent element node.

Example — Creating an XML File with xmlwrite

Suppose that you want to create an info.xml file for the Upslope Area Toolbox (described in Display Custom Documentation), as follows:

<?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>

To create this file using xmlwrite, follow these steps:

  1. Create the document node and root element, toc:

    docNode = com.mathworks.xml.XMLUtils.createDocument('toc');
  2. Identify the root element, and set the version attribute:

    toc = docNode.getDocumentElement;
  3. 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'));
  4. Add the comment:

    product.appendChild(docNode.createComment(' Functions '));
  5. 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.
  6. Export the DOM node to info.xml, and view the file with the type function:


Updating an Existing XML File

To change data in an existing file, call xmlread to import the file into a DOM node. Traverse the node and add or change data using methods defined by the World Wide Web consortium, such as:

  • getElementsByTagName

  • getFirstChild

  • getNextSibling

  • getNodeName

  • getNodeType

When the DOM node contains all your changes, call xmlwrite to overwrite the file.

For a complete list of the methods and properties of DOM nodes, see the org.w3c.dom package description at http://download.oracle.com/javase/6/docs/api/.

For examples that use these methods, see:

Was this topic helpful?