Main Content


Read XML document and return Document Object Model node



DOMnode = xmlread(filename) reads the specified XML file and returns an Apache® Xerces-J document object representing a parsed version of the XML file. Apache Xerces-J implements the Java® API for XML Processing (JAXP). Use JAXP functions to manipulate this document object. For more information on Apache Xerces-J, see


DOMnode = xmlread(filename,'AllowDoctype',tf) also specifies whether DOCTYPE declarations are permitted. If tf is false, reading an input XML file containing DOCTYPE declarations results in an error. Otherwise, xmlread returns an output DOMnode for the XML file. The default value of tf is true.


collapse all

Examine the contents of a sample XML file and then read the XML file into a Document Object Model (DOM) node.

Display the contents of the file sample.xml.

sampleXMLfile = 'sample.xml';

<name>Example Manager</name>

<label>Example Manager</label>


Read the XML file into a DOM node.

DOMnode = xmlread(sampleXMLfile);

Create a parsing function to read an XML file into a MATLAB® structure, and then read a sample XML file into the MATLAB workspace.

To create the function parseXML, copy and paste this code into an m-file parseXML.m. The parseXML function parses data from an XML file into a MATLAB structure array with fields Name, Attributes, Data, and Children.

function theStruct = parseXML(filename)
% PARSEXML Convert XML file to a MATLAB structure.
   tree = xmlread(filename);
   error('Failed to read XML file %s.',filename);

% Recurse over child nodes. This could run into problems 
% with very deeply nested trees.
   theStruct = parseChildNodes(tree);
   error('Unable to parse XML file %s.',filename);

% ----- Local function PARSECHILDNODES -----
function children = parseChildNodes(theNode)
% Recurse over node children.
children = [];
if theNode.hasChildNodes
   childNodes = theNode.getChildNodes;
   numChildNodes = childNodes.getLength;
   allocCell = cell(1, numChildNodes);

   children = struct(             ...
      'Name', allocCell, 'Attributes', allocCell,    ...
      'Data', allocCell, 'Children', allocCell);

    for count = 1:numChildNodes
        theChild = childNodes.item(count-1);
        children(count) = makeStructFromNode(theChild);

% ----- Local function MAKESTRUCTFROMNODE -----
function nodeStruct = makeStructFromNode(theNode)
% Create structure of node info.

nodeStruct = struct(                        ...
   'Name', char(theNode.getNodeName),       ...
   'Attributes', parseAttributes(theNode),  ...
   'Data', '',                              ...
   'Children', parseChildNodes(theNode));

if any(strcmp(methods(theNode), 'getData'))
   nodeStruct.Data = char(theNode.getData); 
   nodeStruct.Data = '';

% ----- Local function PARSEATTRIBUTES -----
function attributes = parseAttributes(theNode)
% Create attributes structure.

attributes = [];
if theNode.hasAttributes
   theAttributes = theNode.getAttributes;
   numAttributes = theAttributes.getLength;
   allocCell = cell(1, numAttributes);
   attributes = struct('Name', allocCell, 'Value', ...

   for count = 1:numAttributes
      attrib = theAttributes.item(count-1);
      attributes(count).Name = char(attrib.getName);
      attributes(count).Value = char(attrib.getValue);

Use the parseXML function to parse the sample file info.xml into a MATLAB structure.

sampleXMLfile = 'info.xml';
mlStruct = parseXML(sampleXMLfile)
mlStruct = struct with fields:
          Name: 'productinfo'
    Attributes: [1x2 struct]
          Data: ''
      Children: [1x13 struct]

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

Version History

Introduced before R2006a