Hauptinhalt

matlab.io.xml.dom.WriterConfiguration Class

Namespace: matlab.io.xml.dom

XML DOM writer options

Description

Use matlab.io.xml.dom.WriterConfiguration objects to configure an XML DOM writer represented as a matlab.io.xml.dom.DOMWriter object. Creating a matlab.io.xml.dom.DOMWriter object creates a WriterConfiguration object. Use the Configuration property of the DOMWriter object to access the WriterConfiguration object.

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

Class Attributes

HandleCompatible
true
ConstructOnLoad
true

For information on class attributes, see Class Attributes.

Properties

expand all

Whether to pretty-print the output XML markup, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — The writer formats the entire document. If the document was previously pretty-printed, using this option can lead to extra white space between the elements. To reformat a formatted document, delete the white space between elements.

  • 0 (false) — The writer does not format the document.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Whether to add blank lines after top-level page elements when pretty-printing, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — The writer adds a line of white space after every direct child of the XML document root.

  • 0 (false) — The writer does not add a line of white space after every direct child of the XML document root.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Whether to include an XML declaration in the output, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — The writer includes an XML declaration in the output.

  • 0 (false) — The writer does not include an XML declaration from the output.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Whether to include a Document Type Definition (DTD) in the output, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — If the DOM document also contains a Document Type Declaration (DTD), the writer includes the DTD in the XML file output.

  • 0 (false) — The writer does not include the DTD in the XML file output.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Whether to preserve entity references in the output file, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — The writer preserves the entity references in the output file.

  • 0 (false) — The writer replaces the entity reference with the content of the entity that it references in the output file. This property applies only to the entities that the XML document type declaration defines. It does not apply to XML markup entities such as & (ampersand).

Note

This property applies only when the document is parsed with a parser configured to preserve entity references defined in the doctype, for example matlab.io.xml.Parser.Configuration.Entities = true;.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Whether to include a byte order mark (BOM) in the output, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — The writer includes a BOM at the beginning of the XML file output stream.

  • 0 (false) — The writer does not include a BOM at the beginning of the XML file output stream.

The writer includes a BOM only if it writes to a file and the output encoding is one of these encodings:

  • UTF-8

  • UTF-16

  • UTF-16LE

  • UTF-16BE

  • UCS-4

  • UCS-4LE

  • UCS-4BE

For UTF-16 and UCS-4 encodings, the endian mode of the host machine determines the BOM.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Whether to discard default content in the output, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — The writer uses available information, such as an XML schema, DTD, and the specified flag on Attr nodes, to decide which attributes and content to discard.

  • 0 (false) — The writer keeps all attributes and content.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Whether to split CDATA sections, specified as a logical 1 (true) or 0 (false). When you specify:

  • 1 (true) — The writer splits CDATA sections that contain the CDATA section termination marker ']]>' or unrepresentable characters in the output encoding.

  • 0 (false) — The writer returns an error if a CDATA section contains a CDATA section termination marker ']]>' or an unrepresentable character.

Attributes:

GetAccess
public
SetAccess
public
NonCopyable
true

Data Types: logical

Examples

collapse all

Write an XML document to a file and and apply pretty-print formatting on the XML.

Create an XML document.

import matlab.io.xml.dom.*
docNode = Document("root_element");
docRootNode = getDocumentElement(docNode);
weekdays = ["Mon" "Tue" "Wed" "Thu" "Fri"];
weekdaysElement = createElement(docNode,"weekdays");
for i=1:5
    dayElement = createElement(docNode,"day");
    appendChild(dayElement,createTextNode(docNode,weekdays(i)));
    appendChild(weekdaysElement,dayElement);
end
appendChild(docRootNode,weekdaysElement);

Create a writer to serialize the XML document.

xmlFileName = "weekdays.xml";
writer = matlab.io.xml.dom.DOMWriter;

Enable pretty-print formatting.

writer.Configuration.FormatPrettyPrint = true;
writeToFile(writer,docNode,xmlFileName);

View the formatted file.

type(xmlFileName);
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<root_element>
  <weekdays>
    <day>Mon</day>
    <day>Tue</day>
    <day>Wed</day>
    <day>Thu</day>
    <day>Fri</day>
  </weekdays>
</root_element>

Specify entity references in the output XML file.

Create an input file for the writer.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
  <!ENTITY extEntity SYSTEM "external.txt">
]>
<root>
  <message>&extEntity;</message>
</root>

Create a text file named external.txt that contains the content Hello World.

Hello World

Parse the XML file.

import matlab.io.xml.dom.*
xmlFile = "input.xml";  % Replace with your actual file path
parser = Parser();
parser.Configuration.AllowDoctype = true;
parser.Configuration.Entities = true;
doc = parseFile(parser, xmlFile);

Create a DOM writer object.

writer = DOMWriter();

To preserve entity references in the output file, set the Configuration Entities property to true and write the output XML file.

writer.Configuration.Entities = true;
writeToFile(writer,doc,"output.xml");

Open the generated XML. Since Configuration Entities property is true, the <message> tags contain the entity reference, &extEntity;.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE root [
  <!ENTITY extEntity SYSTEM "external.txt">
]><root>
  <message>&extEntity;</message>
</root>

To replace the entity reference with the content of the entity that it references, set the Configuration.Entities property to false and write the output XML file again.

writer.Configuration.Entities = false;
writeToFile(writer,doc,"output.xml");

Since Configuration Entities property is false, the content in the <message> tags contain the "Hello World" content from the referenced file, external.txt.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE root [
  <!ENTITY extEntity SYSTEM "external.txt">
]><root>
  <message>Hello World</message>
</root>

Version History

Introduced in R2021a