| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
DOMnode = xmlread(filename)
DOMnode = xmlread(filename) reads a URL or filename and returns a Document Object Model node representing the parsed document. The filename input is a string enclosed in single quotes. The node can be manipulated by using standard DOM functions.
A properly parsed document displays to the screen as
xDoc = xmlread(...)
xDoc =
[#document: null]Find out more about the Document Object Model at the World Wide Web Consortium (W3C®) Web site, http://www.w3.org/DOM/. For specific information on using Java DOM objects, visit the Sun Web site, http://www.java.sun.com/xml/docs/api.
All XML files have a single root element. Some XML files declare a preferred schema file as an attribute of this element. Use the getAttribute method of the DOM node to get the name of the preferred schema file:
xDoc = xmlread(fullfile(matlabroot, ...
'toolbox/matlab/general/info.xml'));
xRoot = xDoc.getDocumentElement;
schemaURL = ...
char(xRoot.getAttribute('xsi:noNamespaceSchemaLocation'))
schemaURL =
http://www.mathworks.com/namespace/info/v1/info.xsdEach info.xml file on the MATLAB path contains several listitem elements with a label and callback element. This script finds the callback that corresponds to the label 'Plot Tools':
infoLabel = 'Plot Tools';
infoCbk = '';
itemFound = false;
xDoc = xmlread(fullfile(matlabroot, ...
'toolbox/matlab/general/info.xml'));
% Find a deep list of all listitem elements.
allListItems = xDoc.getElementsByTagName('listitem');
% Note that the item list index is zero-based.
for k = 0:allListItems.getLength-1
thisListItem = allListItems.item(k);
childNode = thisListItem.getFirstChild;
while ~isempty(childNode)
%Filter out text, comments, and processing instructions.
if childNode.getNodeType == childNode.ELEMENT_NODE
% Assume that each element has a single
% org.w3c.dom.Text child.
childText = char(childNode.getFirstChild.getData);
switch char(childNode.getTagName)
case 'label';
itemFound = strcmp(childText, infoLabel);
case 'callback' ;
infoCbk = childText;
end
end % End IF
childNode = childNode.getNextSibling;
end % End WHILE
if itemFound
break;
else
infoCbk = '';
end
end % End FOR
disp(sprintf('Item "%s" has a callback of "%s".', ...
infoLabel, infoCbk))This function parses an XML file using methods of the DOM node returned by xmlread, and stores the data it reads in the Name, Attributes, Data, and Children fields of a MATLAB structure:
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![]() | xlswrite | xmlwrite | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |