xmlread - Read XML document and return Document Object Model node - MATLAB (original) (raw)
Read XML document and return Document Object Model node
Syntax
Description
DOMnode = xmlread([filename](#mw%5F8979f41c-e051-4cda-b935-6c907d870387))
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 https://xerces.apache.org/xerces-j/apiDocs/.
DOMnode = xmlread([filename](#mw%5F8979f41c-e051-4cda-b935-6c907d870387),'AllowDoctype',tf)
also specifies whether DOCTYPE declarations are permitted. If tf
isfalse
, reading an input XML file containing DOCTYPE declarations results in an error. Otherwise, xmlread
returns an outputDOMnode
for the XML file. The default value of tf
istrue
.
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)
R2012a Example Manager internal ApplicationIcon.DEMOS
Example Manager com.mathworks.xwidgets.ExampleManager.showViewer ApplicationIcon.DEMOSRead 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
, andChildren
.
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 fileinfo.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.
Data Types: char
| string
Version History
Introduced before R2006a