文档

xmlread

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

语法

DOMnode = xmlread(文件名)

描述

DOMnode= xmlread (文件名读取指定的XML文件并返回表示文档的文档对象模型节点。

输入参数

文件名

指定本地文件或URL名称的字符向量。

输出参数

DOMnode

文档对象模型节点,由万维网联盟定义。有关更多信息,请参见什么是XML文档对象模型(DOM)?

例子

XML文件中的根元素有时包括xsi: noNamespaceSchemaLocation属性。此属性的值是首选模式文件的名称。调用getAttribute方法获取此值:

xDoc = xmlread (fullfile (matlabroot,“工具箱”,…“matlab”、“一般”、“info.xml '));xRoot = xDoc.getDocumentElement;模式= char (xRoot.getAttribute (xsi: noNamespaceSchemaLocation))

这段代码的回报:

模式= //www.tatmou.com/namespace/info/v1/info.xsd

创建将XML文件中的数据解析到MATLAB中的函数®带字段的结构阵列的名字属性数据,孩子们

将XML文件转换为MATLAB结构。Try tree = xmlread(文件名);catch错误('Failed to read XML file %s.',filename);递归遍历子节点。这可能会在嵌套非常深的树中遇到问题。try theStruct = parseChildNodes(tree);catch错误('Unable to parse XML file %s.',filename);end % -----本地函数PARSECHILDNODES ----- function children = PARSECHILDNODES (node) %递归遍历节点的子节点。孩子= [];如果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

提示

正确解析的文档的显示如下【#文档:零】.例如,

xDoc = xmlread(“info.xml”)
返回

xDoc = [#document: null]

之前介绍过的R2006a

这个话题有用吗?