xmlwrite
Write XML Document Object Model node
Description
Examples
Write an XML file by, first, creating a Document Object Model (DOM) node containing the XML data. Then, write the DOM node to an XML file. The final XML file should contain this text.
<?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>
First, create the DOM node object and root element, and populate the elements and the attributes of the node corresponding to the XML data.
docNode = com.mathworks.xml.XMLUtils.createDocument('toc');Identify the root element, and set the version attribute.
toc = docNode.getDocumentElement; toc.setAttribute('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 = docNode.createElement('tocitem'); product.setAttribute('target','upslope_product_page.html'); product.appendChild(docNode.createTextNode('Upslope Area Toolbox')); toc.appendChild(product);
Add comment.
product.appendChild(docNode.createComment(' Functions '));Add a tocitem element node for each function, where the target is of the form function_help.html.
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);
endFinally, export the DOM node to an XML file named infoUAT.xml, and view the file using the type function.
xmlwrite('infoUAT.xml',docNode); type('infoUAT.xml');
Read a DOM node from a sample XML file and get the contents of the DOM node as a character vector.
Display the contents of the sample XML file, and then import the DOM node from the file.
sampleXMLfile = 'sample.xml';
type(sampleXMLfile)
DOMnode = xmlread(sampleXMLfile);Use xmlwrite to return the DOMnode object as a serialized character vector.
text = xmlwrite(DOMnode)
text =
'<?xml version="1.0" encoding="utf-8"?>
<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>'
Input Arguments
File name, specified as a character vector or string scalar containing the name of the local file or URL.
Data Types: char | string
Document Object Model (DOM) node, specified as a DOM node object. You can specify the DOM node as a Java® API for XML Processing (JAXP) DOM object or a MATLAB API for XML Processing (MAXP) DOM object (since R2026a).
Document Object Model is defined by the World Wide Web consortium. For more information, see The XML Document Object Model. For more information on the Java API, see Java Platform API Specification.
Version History
Introduced before R2006aWhen writing data to an XML file using the xmlwrite function, you can
specify a MATLAB API for XML Processing (MAXP) Document Object Model (DOM) node object as
the DOMnode input argument. Previously, the function accepted only a
Java API for XML Processing (JAXP) DOM object.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)