Writer that creates a text file
Use an object of the
matlab.io.xml.dom.FileWriter class to create a writer
that streams text to a file. Use a
matlab.io.xml.dom.FileWriter writer with a
matlab.io.xml.dom.DOMWriter writer to mix serialized XML output with output from
other text sources.
You cannot create a writer for a file that is already open in another writer. Use the
close method to release a file that a writer creates.
matlab.io.xml.dom.FileWriter class is a
For information on class attributes, see Class Attributes.
filePath— Path and file name
Path and file name of the file to which the writer streams text, specified as a character vector or string scalar. The file must be writable.
FileEncoding— Encoding of text output
Encoding of the text output, specified as a character vector or string scalar. To set this property value, use the constructor that takes the encoding as an input argument.
Specify the text as a character vector or string scalar.
matlab.io.xml.dom.FileWriter object with a
matlab.io.xml.dom.DOMWriter object to mix serialized XML output with output from other text sources. This example wraps the XML output with text that marks the beginning and end of the XML.
Create an XML document.
import matlab.io.xml.dom.* docNode = Document("root_element"); docRootNode = getDocumentElement(docNode); setAttribute(docRootNode,"attribute","attribute_value"); for i=1:20 thisElement = createElement(docNode,"child_node"); appendChild(thisElement,createTextNode(docNode,sprintf("%i",i))); appendChild(docRootNode,thisElement); end appendChild(docNode,createComment(docNode,"this is a comment"));
matlab.io.xml.dom.FileWriter object and use the
write method to write text to a file.
fileWriter = FileWriter('mixed.txt'); write(fileWriter,"Start of XML content:"+newline);
matlab.io.xml.dom.DOMWriter object and call the
write method to write the serialized XML to the same file.
Write more text to the file.
write(fileWriter,newline+"End of XML content");