You can import an XML file into a Document Object Model (DOM) document node by using a
matlab.io.xml.dom.Parser object or the xmlread function.
The matlab.io.xml.dom.Parser class belongs to the MATLAB® API for XML Processing (MAXP). When you use a MAXP Parser
object to read an XML file, the resulting DOM document node is represented as a matlab.io.xml.dom.Document object. For a list of the classes that you can
use to work with a Document object, see matlab.io.xml.dom. You do not need Java® software to use MAXP classes.
To work with a DOM document node object created by xmlread, you
must use the Java API for XML Processing (JAXP). For a list of the JAXP methods and
properties, see the org.w3c.dom package description at https://docs.oracle.com/javase/7/docs/api.
In a Document Object Model, every item in an XML file corresponds to a node. The properties and methods that you use to 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 DOM nodes:
Element nodes — Correspond to tag names. In
the 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 — Contain 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 — Contain 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 — Include additional text in
the file, in the form <!--Sample comment-->.
Document nodes — Correspond to the entire file. Use methods on the document node to create new element, text, attribute, or comment nodes.
This example uses a matlab.io.xml.dom.Parser object to read an
info.xml file into a matlab.io.xml.dom.Document node. The file contains several
listitem elements. Each listitem
element contains a label and callback
element. The example uses MAXP methods to find the text content of the
callback element that corresponds to the
label that has text content Plot
Tools.
Read the file into a Document object.
infoFile = fullfile(matlabroot,'toolbox/matlab/general/info.xml'); infoLabel = 'Plot Tools'; infoCbk = ''; itemFound = false; import matlab.io.xml.dom.* xDoc = parseFile(Parser,infoFile);
Find all the listitem elements by calling the
getElementsByTagName method, which returns a matlab.io.xml.dom.NodeList object.
allListItems = getElementsByTagName(xDoc,'listitem');For each listitem element, compare the text of the
label element to Plot Tools. When
you locate the correct label, get the callback text. To
access an element in the NodeList object, use the
node method, which uses one-based indexing.
Alternatively, you can use the item method, which uses
zero-based indexing.
length = allListItems.Length; for i=1:length thisListItem = node(allListItems,i); childNode = getFirstChild(thisListItem); while ~isempty(childNode) %Filter out text, comments, and processing instructions. if isa(childNode,'matlab.io.xml.dom.Element') %Assume that each element has a single Text child childText = getData(getFirstChild(childNode)); switch getTagName(childNode) case 'label' itemFound = strcmp(childText,infoLabel); case 'callback' infoCbk = childText; end end childNode = getNextSibling(childNode); end if itemFound break else infoCbk = ''; end end
Display the result.
fprintf('Item "%s" has a callback of "%s".\n', infoLabel,infoCbk);Item "Plot Tools" has a callback of "figure; plottools".
xmlreadThis example uses xmlread to read the
info.xml file into a DOM document node and Java API for XML Processing methods to find the text content of the
callback element that corresponds to the
label that has text content Plot
Tools.
infoFile = fullfile(matlabroot,'toolbox/matlab/general/info.xml'); infoLabel = 'Plot Tools'; infoCbk = ''; itemFound = false; xDoc = xmlread(infoFile); allListItems = getElementsByTagName(xDoc,'listitem'); %The item list index is zero-based. length = allListItems.getLength-1; for i=0:length thisListItem = item(allListItems,i); childNode = getFirstChild(thisListItem); 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 childNode = getNextSibling(childNode); end if itemFound break else infoCbk = ''; end end fprintf('Item "%s" has a callback of "%s".\n', infoLabel,infoCbk);
Item "Plot Tools" has a callback of "figure; plottools".
matlab.io.xml.dom.Document | xmlread