| Contents | Index |
DOMnode= xmlread(filename)
DOMnode= xmlread(filename) reads the specified XML file and returns a Document Object Model node representing the document.
The display for a properly parsed document is [#document: null]. For example,
xDoc = xmlread('info.xml')returns
xDoc = [#document: null]
filename |
String enclosed in single quotation marks that specifies the name of the local file or URL. |
DOMnode |
Document Object Model node, as defined by the World Wide Web consortium. For more information, see What Is an XML Document Object Model (DOM)?. |
The root element in an XML file sometimes includes an xsi:noNamespaceSchemaLocation attribute. The value of this attribute is the name of the preferred schema file. Call the getAttribute method to get this value:
xDoc = xmlread(fullfile(matlabroot,'toolbox',...
'matlab','general','info.xml'));
xRoot = xDoc.getDocumentElement;
schema = char(xRoot.getAttribute('xsi:noNamespaceSchemaLocation'))This code returns:
schema = http://www.mathworks.com/namespace/info/v1/info.xsd
Create functions that parse 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
% ----- Subfunction 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
% ----- Subfunction 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
% ----- Subfunction 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
Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |