Main Content

将文档对象模型导出为 XML 文件

您可以使用 matlab.io.xml.dom.DOMWriter 对象或 xmlwrite 函数将文档对象模型 (DOM) 文档节点导出为 XML 文件。

matlab.io.xml.dom.DOMWriter 类属于用于 XML 处理的 MATLAB® API (MAXP)。要使用 MAXP DOMWriter 对象,请将 DOM 文档节点表示为 matlab.io.xml.dom.Document 对象。要创建元素、文本和其他节点并将其添加到文档节点,请使用 MAXP 类和方法。请参阅 matlab.io.xml.dom。您不需要 Java® 软件便可使用 MAXP 类。

要创建可以使用 xmlwrite 进行编写的 DOM 文档,请使用 com.mathworks.xml.XMLUtils.createDocument。要创建节点并将其添加到文档节点,请使用用于 XML 处理的 Java API (JAXP) 的方法。请参阅 https://docs.oracle.com/javase/7/docs/api 上提供的 org.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 Toolbox 创建一个 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 Toolbox 创建一个 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 中,请使用用于 XML 处理的 MATLAB API (MAXP) 类和方法。请参阅 matlab.io.xml.dom。如果您使用 xmlread,请使用用于 XML 处理的 Java API (JAXP) 方法。请参阅 https://docs.oracle.com/javase/7/docs/api 上提供的 org.w3c.dom 包说明。

  3. 当 DOM 文档包含您的所有更改时,请编写该文件。对于 MAXP DOM 文档,请使用 matlab.io.xml.DOMWriter 对象。对于 JAXP DOM 文档,请使用 xmlwrite

另请参阅

| |

相关主题

外部网站