Main Content

Importing XML Documents

To read an XML file from your local disk or from a URL, use the xmlread function. xmlread returns the contents of the file in a Document Object Model (DOM) node. For more information, see:

What Is an XML Document Object Model (DOM)?

In a Document Object Model, every item in an XML file corresponds to a node. The properties and methods for DOM nodes (that is, the way you create and access nodes) follow standards set by the World Wide Web consortium.

For example, consider this sample XML file:


<!-- This is a sample info.xml file. -->


<label color="blue">Import Wizard</label>

<label color="red">Profiler</label>
<callback>profile viewer</callback>


The information in the file maps to the following types of nodes in a DOM:

  • Element nodes — Corresponds to tag names. In the sample info.xml file, these tags correspond to element nodes:

    • productinfo

    • list

    • listitem

    • label

    • callback

    • icon

    In this case, the list element is the parent of listitem element child nodes. The productinfo element is the root element node.

  • Text nodes — Contains values associated with element nodes. Every text node is the child of an element node. For example, the Import Wizard text node is the child of the first label element node.

  • Attribute nodes — Contains name and value pairs associated with an element node. For example, in the first label element node, color is the name of an attribute and blue is its value. Attribute nodes are not parents or children of any nodes.

  • Comment nodes — Includes additional text in the file, in the form <!--Sample comment-->.

  • Document nodes — Corresponds to the entire file. Use methods on the document node to create new element, text, attribute, or comment nodes.

For a complete list of the methods and properties of DOM nodes, see the org.w3c.dom package description at

Example — Finding Text in an XML File

The full matlabroot/toolbox/matlab/general/info.xml file contains several listitem elements, such as:

<label>Import Wizard</label>

One of the label elements has the child text Plot Tools. Suppose that you want to find the text for the callback element in the same listitem. Follow these steps:

  1. Initialize your variables, and call xmlread to obtain the document node:

    findLabel = 'Plot Tools';
    findCbk = '';
    xDoc = xmlread(fullfile(matlabroot, ...
  2. Find all the listitem elements. The getElementsByTagName method returns a deep list that contains information about the child nodes:

    allListitems = xDoc.getElementsByTagName('listitem');


    Lists returned by DOM methods use zero-based indexing.

  3. For each listitem, compare the text for the label element to the text you want to find. When you locate the correct label, get the callback text:

    for k = 0:allListitems.getLength-1
       thisListitem = allListitems.item(k);
       % Get the label element. In this file, each
       % listitem contains only one label.
       thisList = thisListitem.getElementsByTagName('label');
       thisElement = thisList.item(0);
       % Check whether this is the label you want.
       % The text is in the first child node.
       if strcmp(thisElement.getFirstChild.getData, findLabel)
           thisList = thisListitem.getElementsByTagName('callback');
           thisElement = thisList.item(0);
           findCbk = char(thisElement.getFirstChild.getData);
  4. Display the final results:

    if ~isempty(findCbk)
        msg = sprintf('Item "%s" has a callback of "%s."',...
                      findLabel, findCbk);
        msg = sprintf('Did not find the "%s" item.', findLabel);

For an additional example that creates a structure array to store data from an XML file, see the xmlread function reference page.