Main Content

xmlread

XML ドキュメントを読み取って、ドキュメント オブジェクト モデル ノードを返す

説明

DOMnode = xmlread(filename) は、指定された 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/ を参照してください。

DOMnode = xmlread(filename,'AllowDoctype',tf) は、DOCTYPE 宣言が許可されているかどうかも指定します。tffalse の場合、DOCTYPE 宣言が含まれる入力 XML ファイルを読み取るとエラーになります。それ以外の場合、xmlread は、XML ファイルの出力 DOMnode を返します。tf の既定値は true です。

すべて折りたたむ

サンプル 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 ファイルのデータを解析し、フィールド NameAttributesData、および 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]

入力引数

すべて折りたたむ

ファイル名。ローカル ファイル名または URL を含む、文字ベクトルまたは string スカラーとして指定します。

データ型: char | string

バージョン履歴

R2006a より前に導入