To read an XML file from your local disk or from a URL, use
the xmlread
function. xmlread
returns
the contents of the file in a Document Object Model (DOM) node. For
more information, see:
In a Document Object Model, every item in an XML file corresponds to a node. The properties and methods for DOM nodes (that is, the way you create and access nodes) follow standards set by the World Wide Web consortium.
For example, consider this sample XML file:
<productinfo> <!-- This is a sample info.xml file. --> <list> <listitem> <label color="blue">Import Wizard</label> <callback>uiimport</callback> <icon>ApplicationIcon.GENERIC_GUI</icon> </listitem> <listitem> <label color="red">Profiler</label> <callback>profile viewer</callback> <icon>ApplicationIcon.PROFILER</icon> </listitem> </list> </productinfo>
The information in the file maps to the following types of nodes in a DOM:
Element nodes — Corresponds
to tag names. In the sample info.xml
file, these
tags correspond to element nodes:
productinfo
list
listitem
label
callback
icon
In this case, the list
element is the parent of listitem
element child nodes.
The productinfo
element is the root element
node.
Text nodes — Contains
values associated with element nodes. Every text node is the child
of an element node. For example, the Import Wizard
text
node is the child of the first label
element node.
Attribute nodes — Contains name and value
pairs associated with an element node. For example, in the first
label
element node, color
is the
name of an attribute and blue
is its value. Attribute
nodes are not parents or children of any nodes.
Comment nodes — Includes
additional text in the file, in the form <!--Sample comment-->
.
Document nodes — Corresponds to the entire file. Use methods on the document node to create new element, text, attribute, or comment nodes.
For a complete list of the methods and properties of DOM nodes,
see the org.w3c.dom
package description at https://docs.oracle.com/javase/7/docs/api
.
The full
file
contains several matlabroot
/toolbox/matlab/general/info.xmllistitem
elements, such as:
<listitem> <label>Import Wizard</label> <callback>uiimport</callback> <icon>ApplicationIcon.GENERIC_GUI</icon> </listitem>
One of the label
elements has the child text Plot
Tools
. Suppose that you want to find the text for the callback
element
in the same listitem
. Follow these steps:
Initialize your variables, and call xmlread
to
obtain the document node:
findLabel = 'Plot Tools'; findCbk = ''; xDoc = xmlread(fullfile(matlabroot, ... 'toolbox','matlab','general','info.xml'));
Find all the listitem
elements. The getElementsByTagName
method
returns a deep list that contains information about the child nodes:
allListitems = xDoc.getElementsByTagName('listitem');
Note
Lists returned by DOM methods use zero-based indexing.
For each listitem
, compare the text
for the label
element to the text you want to find.
When you locate the correct label
, get the callback
text:
for k = 0:allListitems.getLength-1 thisListitem = allListitems.item(k); % Get the label element. In this file, each % listitem contains only one label. thisList = thisListitem.getElementsByTagName('label'); thisElement = thisList.item(0); % Check whether this is the label you want. % The text is in the first child node. if strcmp(thisElement.getFirstChild.getData, findLabel) thisList = thisListitem.getElementsByTagName('callback'); thisElement = thisList.item(0); findCbk = char(thisElement.getFirstChild.getData); break; end end
Display the final results:
if ~isempty(findCbk) msg = sprintf('Item "%s" has a callback of "%s."',... findLabel, findCbk); else msg = sprintf('Did not find the "%s" item.', findLabel); end disp(msg);
For an additional example that creates a structure array to
store data from an XML file, see the xmlread
function
reference page.