Main Content

xmlread

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

说明

示例

DOMnode = xmlread(filename) 读取指定的 XML 文件并返回一个 Apache® Xerces-J 文档对象,该对象表示该 XML 文件的解析版本。Apache Xerces-J 实现用于 XML 处理的 Java® API (JAXP)。使用 JAXP 函数来操作此文档对象。有关 Apache Xerces-J 的详细信息,请参阅 https://xerces.apache.org/xerces-j/apiDocs/

示例

DOMnode = xmlread(filename,'AllowDoctype',tf) 还指定是否允许 DOCTYPE 声明。如果 tffalse,则读取包含 DOCTYPE 声明的输入 XML 文件会导致错误。如果为 true,xmlread 则会为 XML 文件返回输出 DOMnodetf 的默认值是 true

示例

全部折叠

检查示例 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 的字符向量或字符串标量。

数据类型: char | string

版本历史记录

在 R2006a 之前推出