Contenu principal

xmlread

R2026b

Read XML document and return Document Object Model (DOM) node

Description

DOMnode = xmlread(filename) reads the specified XML file and returns a 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 a function 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.

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 returns 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:

  • "maxp" – The default value. Process the XML document using the MATLAB API for XML Processing. If you specify "maxp", xmlread returns a matlab.io.xml.dom.Document.

  • "jaxp" – Process the XML document using the Java API for XML Processing. Apache Xerces-J implements the Java® API for XML Processing (JAXP). For more information on Apache Xerces-J, see Xerces-J API Docs.

Note

The JAXP and MAXP engines return objects with different API surfaces. You might need to rewrite code that calls Java org.w3c.dom helper methods on the returned document object when switching from "jaxp" to "maxp". For example, chained calls that assume methods such as getFirstChild are available on NodeList objects fail under MAXP.

To retrieve the first child of a document in a way that works with both engines, call the method on the node, or use zero-based NodeList access:

root = doc.getFirstChild;
root = doc.getChildNodes.item(0);

For the methods supported by MAXP, see matlab.io.xml.dom.Document and matlab.io.xml.dom.NodeList.

Version History

Introduced before R2006a

expand all