主要内容

xmlread

读取 XML 文档并返回文档对象模型节点

说明

DOMnode = xmlread(filename) 读取指定的 XML 文件并返回一个 Apache® Xerces-J 文档对象,该对象表示该 XML 文件的解析版本。

示例

DOMnode = xmlread(filename,Name=Value) 使用一个或多个名称-值参量指定选项。例如,您可以使用 XMLEngine 指定 XML 处理引擎。

示例

全部折叠

检查示例 XML 文件的内容,然后将该 XML 文件读入文档对象模型 (DOM) 节点中。

显示 sample.xml 文件内容。

sampleXMLfile = 'sample.xml';
type(sampleXMLfile)
<productinfo> 

<matlabrelease>R2012a</matlabrelease>
<name>Example Manager</name>
<type>internal</type>
<icon>ApplicationIcon.DEMOS</icon>

<list>
<listitem>
<label>Example Manager</label>
<callback>com.mathworks.xwidgets.ExampleManager.showViewer
</callback>
<icon>ApplicationIcon.DEMOS</icon>
</listitem>
</list>

</productinfo>

将该 XML 文件读入 DOM 节点中。

DOMnode = xmlread(sampleXMLfile);

创建一个解析函数以将一个 XML 文件读入 MATLAB® 结构体中,然后将一个示例 XML 文件读入 MATLAB 工作区中。

要创建函数 parseXML,请将以下代码复制并粘贴到 m 文件 parseXML.m 中。parseXML 函数将一个 XML 文件中的数据解析为一个 MATLAB 结构体数组,该数组包含字段 NameAttributesDataChildren

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


% ----- Local function 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

% ----- Local function 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

% ----- Local function 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

使用 parseXML 函数将示例文件 info.xml 解析为一个 MATLAB 结构体。

sampleXMLfile = 'info.xml';
mlStruct = parseXML(sampleXMLfile)
mlStruct = struct with fields:
          Name: 'productinfo'
    Attributes: [1x2 struct]
          Data: ''
      Children: [1x13 struct]

输入参数

全部折叠

文件名,指定为包含本地文件名称或 URL 的字符向量或字符串标量。Apache Xerces-J 实现 Java® API for XML Processing (JAXP)。使用 JAXP 函数来操作此文档对象。有关 Apache Xerces-J 的详细信息,请参阅 https://xerces.apache.org/xerces-j/apiDocs/

数据类型: char | string

名称-值参数

全部折叠

将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

示例: d = xmlread(filename,AllowDoctype=false) 不允许使用 DOCTYPE 声明

允许使用 DOCTYPE 声明,指定为数值或逻辑值 1 (true) 或 0 (false)。如果指定为 true,则 xmlread 将返回 XML 文件的一个输出 DOM 节点,而不考虑 DOCTYPE 声明。如果指定为 false,则在 XML 文件包含 DOCTYPE 声明时 xmlread 将出错。

自 R2026a 起

XML 处理引擎,指定为以下值之一:

  • "jaxp" - 默认值。使用 Java API for XML Processing 处理 XML 文档。

  • "maxp" - 使用 MATLAB API for XML Processing 处理 XML 文档。当指定 "maxp" 时,返回值是 matlab.io.xml.dom.Document

版本历史记录

在 R2006a 之前推出

全部展开