Main Content

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

matlab.io.xml.dom.DOMWriter オブジェクトまたは関数 xmlwrite を使用することで、ドキュメント オブジェクト モデル (DOM) ドキュメント ノードを XML ファイルにエクスポートできます。

matlab.io.xml.dom.DOMWriter クラスは、MATLAB® API for XML Processing (MAXP) に属しています。MAXP DOMWriter オブジェクトを使用するには、DOM ドキュメント ノードを matlab.io.xml.dom.Document オブジェクトとして表現します。要素、テキスト、その他のノードを作成してドキュメント ノードに追加するには、MAXP クラスおよびメソッドを使用します。matlab.io.xml.dom を参照してください。MAXP クラスを使用するのに Java® ソフトウェアは必要ありません。

xmlwrite を使用して書き込むことのできる DOM ドキュメントを作成するには、com.mathworks.xml.XMLUtils.createDocument を使用します。ノードを作成してドキュメント ノードに追加するには、Java API for XML Processing (JAXP) のメソッドを使用します。https://docs.oracle.com/javase/7/docs/apiorg.w3c.dom パッケージの説明を参照してください。

DOM ドキュメントの作成

XML ドキュメントを作成する一般的な手順は次のとおりです。

  1. ドキュメント ノードを作成し、ルート要素を定義します。次のコードは MAXP matlab.io.xml.dom.Document オブジェクトを作成することにより、ドキュメント ノードを作成します。

    import matlab.io.xml.dom.*
    docNode = Document('root_element');

    次のコードは、JAXP メソッドで使用できるドキュメント ノードを作成します。

    docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
  2. getDocumentElement を呼び出して、ルート要素に対応するノードを取得します。ルート要素ノードは子ノードの追加に必要です。

  3. ドキュメント ノードのメソッドを呼び出して、要素、テキスト、コメント、および属性の各ノードを追加します。有用なメソッドには次のものがあります。

    • createElement

    • createTextNode

    • createComment

    • setAttribute

  4. 親ノードに子ノードを追加するには、appendChild を使用します。

    ヒント

    テキスト ノードは常に要素ノードの子となります。テキスト ノードを追加するには、ドキュメント ノードで createTextNode を使用し、次に親要素ノードで appendChild を使用します。

MAXP DOMWriter オブジェクトを使用した DOM ドキュメント ノードの XML ファイルへの書き込み

この例では、matlab.io.xml.dom.DOMWriter オブジェクトを使用して、カスタム ドキュメンテーションの表示に記載されている Upslope Area ツールボックス用の info.xml ファイルを作成します。

ドキュメント ノードとルート要素 toc を作成します。

import matlab.io.xml.dom.*
docNode = Document('toc');

ルート要素を取得し、version 属性を設定します。

toc = docNode.getDocumentElement;
setAttribute(toc,'version','2.0');

tocitem 要素ノードを製品ページに追加します。このファイルの各 tocitem 要素には、target 属性と子テキスト ノードが設けられています。

product = createElement(docNode,'tocitem');
setAttribute(product,'target','upslope_product_page.html');
appendChild(product,createTextNode(docNode,'Upslope Area Toolbox'));
appendChild(toc,product);

コメントを追加します。

appendChild(product,createComment(docNode,' Functions '));

各関数に tocitem 要素ノードを追加します。

functions = {'demFlow','facetFlow','flowMatrix','pixelFlow'};
n = numel(functions);
for idx = 1:n
    curr_node = createElement(docNode,'tocitem');
    curr_file = [functions{idx} '_help.html']; 
    setAttribute(curr_node,'target',curr_file);
    
    % Child text is the function name.
   appendChild(curr_node,createTextNode(docNode,functions{idx}));
    appendChild(product,curr_node);
end

DOM ノードを info.xml にエクスポートし、ファイルを表示します。

xmlFileName = 'info.xml';
writer = matlab.io.xml.dom.DOMWriter;
writer.Configuration.FormatPrettyPrint = true;

writeToFile(writer,docNode,xmlFileName);

type('info.xml');

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<toc version="2.0">

  <tocitem target="upslope_product_page.html">Upslope Area Toolbox
    <!-- Functions -->
    <tocitem target="demFlow_help.html">demFlow</tocitem>
    <tocitem target="facetFlow_help.html">facetFlow</tocitem>
    <tocitem target="flowMatrix_help.html">flowMatrix</tocitem>
    <tocitem target="pixelFlow_help.html">pixelFlow</tocitem>
  </tocitem>

</toc>

xmlwrite を使用した DOM ドキュメント ノードの XML ファイルへの書き込み

この例では、xmlwrite を使用して、カスタム ドキュメンテーションの表示に記載されている Upslope Area ツールボックス用の info.xml ファイルを作成します。

docNode = com.mathworks.xml.XMLUtils.createDocument('toc');
toc = docNode.getDocumentElement;
toc.setAttribute('version','2.0');
product = docNode.createElement('tocitem');
product.setAttribute('target','upslope_product_page.html');
product.appendChild(docNode.createTextNode('Upslope Area Toolbox'));
toc.appendChild(product)
product.appendChild(docNode.createComment(' Functions '));
functions = {'demFlow','facetFlow','flowMatrix','pixelFlow'};
for idx = 1:numel(functions)
    curr_node = docNode.createElement('tocitem');
    
    curr_file = [functions{idx} '_help.html']; 
    curr_node.setAttribute('target',curr_file);
    
    % Child text is the function name.
    curr_node.appendChild(docNode.createTextNode(functions{idx}));
    product.appendChild(curr_node);
end
xmlwrite('info.xml',docNode);
type('info.xml');
<?xml version="1.0" encoding="utf-8"?>
<toc version="2.0">
   <tocitem target="upslope_product_page.html">Upslope Area Toolbox<!-- Functions --><tocitem target="demFlow_help.html">demFlow</tocitem>
      <tocitem target="facetFlow_help.html">facetFlow</tocitem>
      <tocitem target="flowMatrix_help.html">flowMatrix</tocitem>
      <tocitem target="pixelFlow_help.html">pixelFlow</tocitem>
   </tocitem>
</toc>

既存の XML ファイルの更新

既存のファイルのデータを変更するには、以下を行います。

  1. matlab.io.xml.dom.Parser オブジェクトまたは xmlread を使用して、ファイルを DOM ドキュメント ノードにインポートします。

  2. 以下のようなメソッドを使用してノードを詳しく調べ、データの追加や変更を行います。

    • getElementsByTagName

    • getFirstChild

    • getNextSibling

    • getNodeName

    • getNodeType

    matlab.io.xml.dom.Parser を使用して XML ファイルを matlab.io.xml.dom.Document に読み込む場合、MATLAB API for XML Processing (MAXP) のクラスおよびメソッドを使用します。matlab.io.xml.dom を参照してください。xmlread を使用する場合、Java API for XML Processing (JAXP) のメソッドを使用します。https://docs.oracle.com/javase/7/docs/apiorg.w3c.dom パッケージの説明を参照してください。

  3. DOM ドキュメントにすべての変更が含まれている場合は、ファイルに書き込みます。MAXP DOM ドキュメントの場合、matlab.io.xml.DOMWriter オブジェクトを使用します。JAXP DOM ドキュメントの場合、xmlwrite を使用します。

参考

| |

関連するトピック

外部の Web サイト