Export a Document Object Model to an XML File
You can export a Document Object Model (DOM) document node to an XML file by using a
matlab.io.xml.dom.DOMWriter
object or the xmlwrite
function.
The matlab.io.xml.dom.DOMWriter
class belongs to the MATLAB® API for XML Processing (MAXP). To use a MAXP DOMWriter
object, represent a DOM document node as a matlab.io.xml.dom.Document
object. To create and add elements, text, and
other nodes to the document node, use MAXP classes and methods. See matlab.io.xml.dom
. You do not need Java® software to use MAXP classes.
To create a DOM document that you can write by using xmlwrite
,
use com.mathworks.xml.XMLUtils.createDocument
. To create and add nodes
to the document node, use methods of the Java API for XML Processing (JAXP). See the org.w3c.dom
package description at https://docs.oracle.com/javase/7/docs/api
.
Create a DOM Document
Common steps for creating an XML document include:
Create a document node and define the root element. This code creates a document node by creating a MAXP
matlab.io.xml.dom.Document
object:import matlab.io.xml.dom.* docNode = Document('root_element');
This code creates a document node that you can use with JAXP methods:
docNode = com.mathworks.xml.XMLUtils.createDocument('root_element');
Get the node that corresponds to the root element by calling
getDocumentElement
. The root element node is required to add child nodes.Add element, text, comment, and attribute nodes by calling methods of the document node. Useful methods include:
createElement
createTextNode
createComment
setAttribute
To append child nodes to a parent node, use
appendChild
.Tip
Text nodes are always children of element nodes. To add a text node, use
createTextNode
with the document node, and then useappendChild
with the parent element node.
Write a DOM Document Node to an XML File Using a MAXP DOMWriter
Object
This example uses a
matlab.io.xml.dom.DOMWriter
object to create an
info.xml
file for the Upslope Area Toolbox, which is
described in Display Custom Documentation.
Create the document node and root element,
toc
.
import matlab.io.xml.dom.* docNode = Document('toc');
Get the root element, and set the version
attribute.
toc = docNode.getDocumentElement; setAttribute(toc,'version','2.0');
Add the tocitem
element node for the product page. Each
tocitem
element in this file has a
target
attribute and a child text
node.
product = createElement(docNode,'tocitem'); setAttribute(product,'target','upslope_product_page.html'); appendChild(product,createTextNode(docNode,'Upslope Area Toolbox')); appendChild(toc,product);
Add a comment.
appendChild(product,createComment(docNode,' Functions '));
Add a tocitem
element node for each
function.
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
Export the DOM node to info.xml
, and view the
file.
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>
Write a DOM Document Node to an XML File Using xmlwrite
This example uses xmlwrite
to create an
info.xml
file for the Upslope Area Toolbox, which is
described in Display Custom Documentation.
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>
Update an Existing XML File
To change data in an existing file:
Import the file into a DOM document node by using a
matlab.io.xml.dom.Parser
object orxmlread
.Traverse the node and add or change data using methods, such as:
getElementsByTagName
getFirstChild
getNextSibling
getNodeName
getNodeType
If you use
matlab.io.xml.dom.Parser
to read the XML file into amatlab.io.xml.dom.Document
, use MATLAB API for XML Processing (MAXP) classes and methods. Seematlab.io.xml.dom
. If you usexmlread
, use Java API for XML Processing (JAXP) methods. See theorg.w3c.dom
package description athttps://docs.oracle.com/javase/7/docs/api
.When the DOM document contains all your changes, write the file. For a MAXP DOM document, use a
matlab.io.xml.DOMWriter
object. For a JAXP DOM document, usexmlwrite
.
See Also
matlab.io.xml.dom.Document
| matlab.io.xml.dom.DOMWriter
| xmlwrite