xmlread
读取 XML 文档并返回文档对象模型节点
说明
DOMnode = xmlread(
读取指定的 XML 文件并返回一个 Apache® Xerces-J 文档对象,该对象表示该 XML 文件的解析版本。Apache Xerces-J 实现用于 XML 处理的 Java® API (JAXP)。使用 JAXP 函数来操作此文档对象。有关 Apache Xerces-J 的详细信息,请参阅 https://xerces.apache.org/xerces-j/apiDocs/。filename
)
示例
将 XML 文件读入文档对象模型 (DOM) 节点中
检查示例 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® 结构体中,然后将一个示例 XML 文件读入 MATLAB 工作区中。
要创建函数 parseXML
,请将以下代码复制并粘贴到 m 文件 parseXML.m
中。parseXML
函数将一个 XML 文件中的数据解析为一个 MATLAB 结构体数组,该数组包含字段 Name
、Attributes
、Data
和 Children
。
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]
输入参数
filename
— 文件名
字符向量 | 字符串标量
文件名,指定为包含本地文件名称或 URL 的字符向量或字符串标量。
数据类型: char
| string
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)