主要内容

XMLREAD.

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

描述

例子

domnode = xmlread(文件名读取指定的XML文件并返回DOMNODE.代表文档的文档对象模型节点。文档对象模型由万维网联盟定义。有关更多信息,请参阅什么是XML文档对象模型(DOM)?

例子

domnode = xmlread(文件名,'allowdoctype',tf)还指定是否允许doctype声明。如果TF.错误的,读取包含doctype声明的输入XML文件会导致错误。除此以外,XMLREAD.返回输出DOMNODE.对于XML文件。默认值TF.真的

例子

全部收缩

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

显示文件的内容sample.xml.

samplexmlfile ='sample.xml';类型(SamplexMlFile)
  r2012a  示例管理器 内部 <图标> applicationicon.demos    

将XML文件读入DOM节点。

domnode = xmlread(samplexmlfile);

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

创建功能parsexml.,将此代码复制并粘贴到m-file中parsexml.m.,或使用parsexml.m.在此示例中包含。这parsexml.函数将数据从XML文件解析为带有字段的MATLAB结构数组名称属性数据, 和孩子们

类型('parsexml.m'
功能Thestuct = ParseXML(文件名)%ParseXML将XML文件转换为Matlab结构。尝试tree = xmlread(文件名);捕获错误('无法读取XML文件%s。',文件名);END%对子节点进行复发。这可能遇到了非常深刻的树木的问题。尝试thestuct = parsechildnodes(树);捕获错误('无法解析XML文件%s。',filename);exp%-----本地函数parseChildnodes -----函数子女= ParseChildNodes(heode)%过重节点儿童。儿童= [];如果bonode.haschildnodes childnodes = whende.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 =.结构与字段:名称:'ProductInfo'属性:[1x2结构]数据:''儿童:[1x13结构]

输入参数

全部收缩

文件名称,指定为字符向量或包含本地文件或URL的名称的字符串标量。

数据类型:char|细绳

在R2006A之前介绍