Main Content

xmlread

Read XML document and return Document Object Model node

Description

DOMnode = xmlread(filename) reads the specified XML file and returns an Apache® Xerces-J document object representing a parsed version of the XML file.

example

DOMnode = xmlread(filename,Name=Value) specifies options using one or more name-value arguments. For example, you can specify the XML processing engine with XMLEngine.

Examples

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';
type(sampleXMLfile)
<productinfo> 

<matlabrelease>R2012a</matlabrelease>
<name>Example Manager</name>
<type>internal</type>
<icon>ApplicationIcon.DEMOS</icon>

<list>
<listitem>
<label>Example Manager</label>
<callback>com.mathworks.xwidgets.ExampleManager.showViewer
</callback>
<icon>ApplicationIcon.DEMOS</icon>
</listitem>
</list>

</productinfo>

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.
try
   tree = xmlread(filename);
catch
   error('Failed to read XML file %s.',filename);
end

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


% ----- 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);
    end
end

% ----- 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); 
else
   nodeStruct.Data = '';
end

% ----- 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', ...
                       allocCell);

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

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. 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 https://xerces.apache.org/xerces-j/apiDocs/.

Data Types: char | string

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: d = xmlread(filename,AllowDoctype=false) does not permit DOCTYPE declarations

Permit DOCTYPE declarations, specified as a numeric or logical 1 (true) or 0 (false). If specified as true, xmlread will return an output DOM node for the XML file regardless of DOCTYPE declarations. If specified as false, xmlread errors if the XML file contains DOCTYPE declarations.

Since R2026a

XML processing engine, specified as one of these values:

  • "jaxp" – The default value. Process the XML document using the Java API for XML Processing.

  • "maxp" – Process the XML document using the MATLAB API for XML Processing. When "maxp" is specified, the returned value is a matlab.io.xml.dom.Document.

Version History

Introduced before R2006a

expand all