xmlread
Read XML document and return Document Object Model node
Description
DOMnode = xmlread( reads the
specified XML file and returns an Apache® Xerces-J document object representing a parsed version of the XML file. filename)
DOMnode = xmlread(
specifies options using one or more name-value arguments. For example, you can specify the
XML processing engine with filename,Name=Value)XMLEngine.
Examples
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
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
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 amatlab.io.xml.dom.Document.
Version History
Introduced before R2006aWhen reading an XML file using the xmlread function, you can specify
the XML processing engine as either the MATLAB API for XML Processing (MAXP) or the Java API
for XML Processing (JAXP). Specify the XMLEngine name-value argument as
"maxp" or "jaxp", respectively.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)