Main Content

XML ファイルのドキュメント オブジェクト モデルへのインポート

matlab.io.xml.dom.Parser オブジェクトまたは関数 xmlread を使用して、XML ファイルをドキュメント オブジェクト モデル (DOM) ドキュメント ノードにインポートできます。

matlab.io.xml.dom.Parser クラスは、MATLAB® API for XML Processing (MAXP) に属しています。MAXP Parser オブジェクトを使用して XML ファイルを読み取ると、得られる DOM ドキュメント ノードは matlab.io.xml.dom.Document オブジェクトとして表現されます。Document オブジェクトで使用できるクラスの一覧については、matlab.io.xml.dom を参照してください。MAXP クラスを使用するのに Java® ソフトウェアは必要ありません。

xmlread を使用して作成された DOM ドキュメント ノード オブジェクトを操作するには、Java API for XML Processing (JAXP) を使用しなければなりません。JAXP のメソッドおよびプロパティの一覧については、https://docs.oracle.com/javase/7/docs/apiorg.w3c.dom パッケージの説明を参照してください。

XML ドキュメント オブジェクト モデル

ドキュメント オブジェクト モデルでは、XML ファイル内のすべての項目がノードに対応します。ノードを作成し、そこにアクセスするために使用するプロパティとメソッドは、World Wide Web consortium によって定められた標準に従っています。

たとえば、次のサンプル XML ファイルについて考えます。

<productinfo>

<!-- This is a sample info.xml file. -->

<list>

<listitem>
<label color="blue">Import Wizard</label>
<callback>uiimport</callback>
<icon>ApplicationIcon.GENERIC_GUI</icon>
</listitem>

<listitem>
<label color="red">Profiler</label>
<callback>profile viewer</callback>
<icon>ApplicationIcon.PROFILER</icon>
</listitem>

</list>
</productinfo>

ファイル内の情報は、以下のタイプの DOM ノードにマップされます。

  • "要素ノード" — タグ名に対応します。info.xml ファイルでは、以下のタグが要素ノードに対応します。

    • productinfo

    • list

    • listitem

    • label

    • callback

    • icon

    この場合、list 要素は、listitem 要素という子ノードの親となります。productinfo 要素はルート要素ノードです。

  • "テキスト ノード" — 要素ノードに関連付けられた値が含まれます。すべてのテキスト ノードは要素ノードの子です。たとえば、Import Wizard テキスト ノードは最初の label 要素ノードの子です。

  • "属性ノード" — 要素ノードに関連付けられた名前/値の組が含まれます。たとえば、最初の label 要素ノードでは、color が属性の名前で blue がその値です。属性ノードは他のノードの親や子になることはありません。

  • "コメント ノード" — ファイル内に <!--Sample comment--> という形式の追加テキストが含まれます。

  • "ドキュメント ノード" — ファイル全体に対応します。ドキュメント ノードでメソッドを使用して、要素、テキスト、属性、またはコメントの各ノードを新たに作成します。

MAXP Parser を使用した XML ファイルの読み取り

この例では、matlab.io.xml.dom.Parser オブジェクトを使用して info.xml ファイルを matlab.io.xml.dom.Document ノードに読み取ります。ファイルにはいくつかの listitem 要素が含まれます。各 listitem 要素には 1 つの label 要素と 1 つの callback 要素が含まれます。この例では、MAXP メソッドを使用して、テキスト コンテンツ Plot Tools をもつ label に対応する callback 要素のテキスト コンテンツを検索します。

ファイルを Document オブジェクトに読み取ります。

infoFile = fullfile(matlabroot,'toolbox/matlab/general/info.xml');
infoLabel = 'Plot Tools';
infoCbk = '';
itemFound = false;

import matlab.io.xml.dom.*
xDoc = parseFile(Parser,infoFile);

getElementsByTagName メソッドを呼び出し、すべての listitem 要素を検索します。これは matlab.io.xml.dom.NodeList オブジェクトを返します。

allListItems = getElementsByTagName(xDoc,'listitem');

listitem 要素について、label 要素のテキストを Plot Tools と比較します。正しいラベルを見つけたら、callback テキストを取得します。NodeList オブジェクト内の要素にアクセスするには、1 ベースのインデックスを使用する node メソッドを使用します。代わりに、0 ベースのインデックスを使用する item メソッドを使用することもできます。

length = allListItems.Length;
for i=1:length

    thisListItem = node(allListItems,i);
    childNode = getFirstChild(thisListItem);

    while ~isempty(childNode)
        %Filter out text, comments, and processing instructions.

        if isa(childNode,'matlab.io.xml.dom.Element')
            %Assume that each element has a single Text child
            
            childText = getData(getFirstChild(childNode));

            switch getTagName(childNode)
                case 'label'
                    itemFound = strcmp(childText,infoLabel);
                case 'callback'
                    infoCbk = childText;
            end
        end
        childNode = getNextSibling(childNode);
    end
    if itemFound
        break
    else
        infoCbk = '';
    end
end

結果を表示します。

fprintf('Item "%s" has a callback of "%s".\n', infoLabel,infoCbk);
Item "Plot Tools" has a callback of "figure; plottools".

xmlread を使用した XML ファイルの読み取り

この例では、xmlread を使用して info.xml ファイルを DOM ドキュメント ノードに読み取り、Java API for XML Processing メソッドを使用してテキスト コンテンツ Plot Tools をもつ label に対応する callback 要素のテキスト コンテンツを検索します。

infoFile = fullfile(matlabroot,'toolbox/matlab/general/info.xml');
infoLabel = 'Plot Tools';
infoCbk = '';
itemFound = false;

xDoc = xmlread(infoFile);

allListItems = getElementsByTagName(xDoc,'listitem');

%The item list index is zero-based.
length = allListItems.getLength-1;
for i=0:length

    thisListItem = item(allListItems,i);
    childNode = getFirstChild(thisListItem);

    while ~isempty(childNode)
        %Filter out text, comments, and processing instructions.

        if childNode.getNodeType == childNode.ELEMENT_NODE
            %Assume that each element has a single org.w3c.dom.Text child

            childText = char(childNode.getFirstChild.getData);

            switch char(childNode.getTagName)
                case 'label'
                    itemFound = strcmp(childText,infoLabel);
                case 'callback'
                    infoCbk = childText;
            end
        end
        childNode = getNextSibling(childNode);
    end
    if itemFound
        break
    else
        infoCbk = '';
    end
end
fprintf('Item "%s" has a callback of "%s".\n', infoLabel,infoCbk);
Item "Plot Tools" has a callback of "figure; plottools".

参考

|

関連するトピック

外部の Web サイト