Main Content

将 XML 文件导入文档对象模型中

您可以使用 matlab.io.xml.dom.Parser 对象或 xmlread 函数将 XML 文件导入文档对象模型 (DOM) 文档节点。

matlab.io.xml.dom.Parser 类属于用于 XML 处理的 MATLAB® API (MAXP)。当您使用 MAXP Parser 对象读取 XML 文件时,生成的 DOM 文档节点表示为 matlab.io.xml.dom.Document 对象。有关可用于处理 Document 对象的类的列表,请参阅 matlab.io.xml.dom。您不需要 Java® 软件便可使用 MAXP 类。

要使用由 xmlread 创建的 DOM 文档节点对象,您必须使用用于 XML 处理的 Java API (JAXP)。有关 JAXP 方法和属性的列表,请参阅 https://docs.oracle.com/javase/7/docs/api 上提供的 org.w3c.dom 包说明。

XML 文档对象模型

在文档对象模型中,XML 文件中的每一项都对应于一个节点。您用于创建和访问节点的属性和方法遵循万维网联盟设定的标准。

例如,考虑以下样本 XML 文件:

<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>

文件中的信息映射到以下类型的 DOM 节点:

  • 元素节点 - 对应于标记名称。在 info.xml 文件中,这些标记对应于元素节点:

    • productinfo

    • list

    • listitem

    • label

    • callback

    • icon

    在此情况下,list 元素是 listitem 元素子节点的父级。productinfo 元素是根元素节点。

  • 文本节点 - 包含与元素节点关联的值。每个文本节点都是一个元素节点的子节点。例如,Import Wizard 文本节点是第一个 label 元素节点的子节点。

  • 属性节点 - 包含与元素节点关联的名称-值对组。例如,在第一个 label 元素节点中,color 是属性的名称,blue 是其值。属性节点不是任何节点的父节点或子节点。

  • 注释节点 - 在文件中包含附加文本,格式为 <!--Sample comment-->

  • 文档节点 - 对应于整个文件。使用文档节点上的方法可创建新的元素、文本、属性或注释节点。

使用 MAXP 解析器读取 XML 文件

此示例使用 matlab.io.xml.dom.Parser 对象将 info.xml 文件读入 matlab.io.xml.dom.Document 节点。该文件包含几个 listitem 元素。每个 listitem 元素包含一个 labelcallback 元素。该示例使用 MAXP 方法来查找 callback 元素的文本内容,该元素对应于具有文本内容 Plot Toolslabel

将该文件读入一个 Document 对象中。

infoFile = fullfile(matlabroot,'toolbox/matlab/general/info.xml');
infoLabel = 'Plot Tools';
infoCbk = '';
itemFound = false;

import matlab.io.xml.dom.*
xDoc = parseFile(Parser,infoFile);

通过调用 getElementsByTagName 方法查找所有 listitem 元素,该方法返回 matlab.io.xml.dom.NodeList 对象。

allListItems = getElementsByTagName(xDoc,'listitem');

对于每个 listitem 元素,都会将 label 元素的文本与 Plot Tools 进行比较。当找到正确的标签时,将获取 callback 的文本。要访问 NodeList 对象中的元素,请使用 node 方法,该方法使用从 1 开始的索引。您也可以使用 item 方法,该方法使用从 0 开始的索引。

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

显示结果。

fprintf('Item "%s" has a callback of "%s".\n', infoLabel,infoCbk);
Item "Plot Tools" has a callback of "figure; plottools".

使用 xmlread 读取 XML 文件

此示例使用 xmlreadinfo.xml 文件读入 DOM 文档节点,并使用用于 XML 处理的 Java API 方法来查找 callback 元素的文本内容,该元素对应于具有文本内容 labelPlot 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".

另请参阅

|

相关主题

外部网站