xmlread
XML ドキュメントを読み取って、ドキュメント オブジェクト モデル ノードを返す
説明
DOMnode = xmlread(
は、指定された XML ファイルを読み取って、解析されたバージョンの XML ファイルを表す Apache® Xerces-J ドキュメント オブジェクトを返します。Apache Xerces-J は Java® API for XML Processing (JAXP) を実装します。JAXP 関数を使用してこのドキュメント オブジェクトを操作します。Apache Xerces-J の詳細については、https://xerces.apache.org/xerces-j/apiDocs/ を参照してください。filename
)
例
XML ファイルをドキュメント オブジェクト モデル (DOM) ノードに読み取る
サンプル 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® 構造体に読み取ってから、サンプル XML ファイルを MATLAB ワークスペースに読み取ります。
関数 parseXML
を作成するには、このコードをコピーして M ファイル parseXML.m
に貼り付ます。関数 parseXML
は XML ファイルのデータを解析し、フィールド Name
、Attributes
、Data
、および Children
をもつ MATLAB 構造体配列に格納します。
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
サンプル ファイル info.xml
を解析して MATLAB 構造体にするには、関数 parseXML
を使用します。
sampleXMLfile = 'info.xml';
mlStruct = parseXML(sampleXMLfile)
mlStruct = struct with fields:
Name: 'productinfo'
Attributes: [1x2 struct]
Data: ''
Children: [1x13 struct]
入力引数
filename
— ファイル名
文字ベクトル | string スカラー
ファイル名。ローカル ファイル名または URL を含む、文字ベクトルまたは string スカラーとして指定します。
データ型: char
| string
バージョン履歴
R2006a より前に導入
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)