xmlread
读取 XML 文档并返回文档对象模型节点
说明
DOMnode = xmlread( 使用一个或多个名称-值参量指定选项。例如,您可以使用 filename,Name=Value)XMLEngine 指定 XML 处理引擎。
示例
检查示例 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 工作区中。
要创建函数 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]
输入参数
文件名,指定为包含本地文件名称或 URL 的字符向量或字符串标量。Apache Xerces-J 实现 Java® API for XML Processing (JAXP)。使用 JAXP 函数来操作此文档对象。有关 Apache Xerces-J 的详细信息,请参阅 https://xerces.apache.org/xerces-j/apiDocs/。
数据类型: char | string
名称-值参数
将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。
示例: d = xmlread(filename,AllowDoctype=false) 不允许使用 DOCTYPE 声明
允许使用 DOCTYPE 声明,指定为数值或逻辑值 1 (true) 或 0 (false)。如果指定为 true,则 xmlread 将返回 XML 文件的一个输出 DOM 节点,而不考虑 DOCTYPE 声明。如果指定为 false,则在 XML 文件包含 DOCTYPE 声明时 xmlread 将出错。
自 R2026a 起
XML 处理引擎,指定为以下值之一:
"jaxp"- 默认值。使用 Java API for XML Processing 处理 XML 文档。"maxp"- 使用 MATLAB API for XML Processing 处理 XML 文档。当指定"maxp"时,返回值是matlab.io.xml.dom.Document。
版本历史记录
在 R2006a 之前推出使用 xmlread 函数读取 XML 文件时,您可以将 XML 处理引擎指定为 MATLAB API for XML Processing (MAXP) 或 Java API for XML Processing (JAXP)。将 XMLEngine 名称-值参量分别指定为 "maxp" 或 "jaxp"。
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)