文件帮助中心
读取XML文档并返回文档对象模型节点
domnode = xmlread(文件名)
domnode = xmlread(filename,'apractdoctype',tf)
例子
domnode = xmlread(文件名)读取指定的XML文件并返回DOMNODE.代表文档的文档对象模型节点。文档对象模型由万维网联盟定义。有关更多信息,请参阅什么是XML文档对象模型(DOM)?。
文件名
DOMNODE.
domnode = xmlread(文件名,'allowdoctype',tf)还指定是否允许doctype声明。如果TF.是错误的,读取包含doctype声明的输入XML文件会导致错误。除此以外,XMLREAD.返回输出DOMNODE.对于XML文件。默认值TF.是真的。
domnode = xmlread(文件名,'allowdoctype',tf)
TF.
错误的
XMLREAD.
真的
全部收缩
检查示例XML文件的内容,然后将XML文件读入文档对象模型(DOM)节点。
显示文件的内容sample.xml.。
sample.xml.
samplexmlfile ='sample.xml';类型(SamplexMlFile)
r2012a matlabrelease> 示例管理器 name> 内部 type> <图标> applicationicon.demos 图标> examplex> manager label> com.mathwands.xwidgets.examplemanager.showViewer 回调> <图标> ApplicationIcon.demos 图标> listitem> list> productInfo>
将XML文件读入DOM节点。
domnode = xmlread(samplexmlfile);
创建解析函数以将XML文件读取到MATLAB®结构,然后将示例XML文件读入MATLAB工作区。
创建功能parsexml.,将此代码复制并粘贴到m-file中parsexml.m.,或使用parsexml.m.在此示例中包含。这parsexml.函数将数据从XML文件解析为带有字段的MATLAB结构数组名称那属性那数据, 和孩子们。
parsexml.
parsexml.m.
名称
属性
数据
孩子们
类型('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结构。
info.xml.
samplexmlfile ='info.xml';mlstruct = parsexml(samplexmlfile)
mlstruct =.结构与字段:名称:'ProductInfo'属性:[1x2结构]数据:''儿童:[1x13结构]
文件名称,指定为字符向量或包含本地文件或URL的名称的字符串标量。
数据类型:char|细绳
char
细绳
XMLWRITE.|XSLT.
XMLWRITE.
XSLT.
系统上存在此示例的修改版本。你想打开这个版本吗?
您单击了与此MATLAB命令对应的链接:
在MATLAB命令窗口中输入它来运行命令。Web浏览器不支持MATLAB命令。金宝app
选择一个网站,以便在可用的地方进行翻译的内容,并查看本地活动和优惠。根据您的位置,我们建议您选择:。
您还可以从以下列表中选择一个网站:
选择中国网站(以中文或英文)以获取最佳网站性能。其他MathWorks国家网站未优化您的位置。
联系您当地的办公室