Main Content

matlab.io.xml.dom.FileWriter Class

Namespace: matlab.io.xml.dom

Writer that creates a text file

Since R2021a

Description

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.

The matlab.io.xml.dom.FileWriter class is a handle class.

Class Attributes

ConstructOnLoad
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Creation

Description

example

writer = matlab.io.xml.dom.FileWriter(filePath) creates a writer that streams text to the file specified by filePath, and sets the FileEncoding property to 'UTF-8'.

writer = matlab.io.xml.dom.FileWriter(filePath,encoding) sets the FileEncoding property to the value of the encoding argument.

Input Arguments

expand all

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.

If the file is stored at a remote location, then filePath must contain the full path of the file specified with the form:

scheme_name://path_to_file/my_file.ext

Based on the remote location, scheme_name can be one of the values in this table.

Remote Locationscheme_name
Amazon S3™s3
Azure® Blob Storagewasb, wasbs
HDFS™hdfs

For more information, see Work with Remote Data.

Example: 's3://bucketname/path_to_file/data.xml'

Properties

expand all

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.

Attributes:

GetAccess
public
SetAccess
immutable
NonCopyable
true

Methods

expand all

Examples

collapse all

Use a 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"));

Create a 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);

Create a matlab.io.xml.dom.DOMWriter object and call the write method to write the serialized XML to the same file.

write(DOMWriter,docNode,fileWriter);

Write more text to the file.

write(fileWriter,newline+"End of XML content");

Version History

Introduced in R2021a