主要内容

xmlread

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

描述

例子

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

例子

DOMnode = xmlread (文件名、“AllowDoctype”tf)还指定了是否允许DOCTYPE声明。如果特遣部队,阅读一个输入XML文件,其中包含DOCTYPE声明会导致错误。否则,xmlread返回一个输出DOMnodeXML文件。的默认值特遣部队真正的

例子

全部折叠

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

显示文件的内容sample.xml

sampleXMLfile =“sample.xml”;类型(sampleXMLfile)
< productinfo > < matlabrelease > R2012a < / matlabrelease > <名称>例子经理< /名称> <类型> < /类型> <图标> ApplicationIcon内部。列演示< /图标> <列表> < > < >标签示例< /标签> <调> com.mathworks.xwidgets.ExampleManager经理。showViewer < /回调> <图标> ApplicationIcon。列演示< /图标> < / > < /列表> < / productinfo >

XML文件读入DOM节点。

DOMnode = xmlread (sampleXMLfile);

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

创建函数parseXML,这段代码复制并粘贴到一个m文件parseXML.m。的parseXML函数解析数据从XML文件到一个MATLAB结构数组字段的名字,属性,数据,孩子们

函数theStruct = parseXML(文件名)% PARSEXML把XML文件转换成一个MATLAB结构。试一试树= xmlread(文件名);错误(无法读取XML文件% s。文件名);结束%递归子节点。这可能会遇到的问题%的深度嵌套树。试一试theStruct = parseChildNodes(树);错误(无法解析XML文件% s。文件名);结束% PARSECHILDNODES - - - - - - - - - - - -本地函数函数孩子= parseChildNodes (theNode)%在节点递归的孩子。孩子= [];如果theNode。hasChildNodes子节点的时候存在缺陷= theNode.getChildNodes;numChildNodes = childNodes.getLength;allocCell =细胞(1、numChildNodes);孩子=结构(“名字”allocCell,“属性”allocCell,“数据”allocCell,“孩子”,allocCell);数= 1:numChildNodes育= childNodes.item(把1);孩子(数)= makeStructFromNode(育);结束结束% MAKESTRUCTFROMNODE - - - - - - - - - - - -本地函数函数nodeStruct = makeStructFromNode (theNode)%创建结构节点信息。nodeStruct =结构(“名字”char (theNode.getNodeName),“属性”parseAttributes (theNode),“数据”,,“孩子”parseChildNodes (theNode));如果任何(strcmp(方法(theNode),“getData”nodeStruct))。Data =字符(theNode.getData);其他的nodeStruct。Data =;结束% PARSEATTRIBUTES - - - - - - - - - - - -本地函数函数属性= parseAttributes (theNode)%创建属性结构。属性= [];如果theNode。hasAttributes theAttributes = theNode.getAttributes;numAttributes = theAttributes.getLength;allocCell =细胞(1、numAttributes);属性=结构(“名字”allocCell,“价值”,allocCell);数= 1:numAttributes鲜明= theAttributes.item(把1);属性(计数)。的名字= char(attrib.getName); attributes(count).Value = char(attrib.getValue);结束结束

使用parseXML函数解析示例文件info.xml成MATLAB结构。

sampleXMLfile =“info.xml”;mlStruct = parseXML (sampleXMLfile)
mlStruct =结构体字段:名称:“productinfo”属性:1 x2结构数据:”孩子:[1 * 13 struct]

输入参数

全部折叠

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

数据类型:字符|字符串

版本历史

之前介绍过的R2006a