writeTaggedText

Helper function for writing _text which has a start tag and end tag on each side and no attributes so that it can be done with one function call instead of three.

writeTaggedText is essentially equivalent to calling

writer.writeStartTag(name, newline);
writer.writeText(text, insertIndent, Newline.no);
writer.writeEndTag(Newline.no);

with the difference being that both the name and text are validated before any data is written. So, if the text is invalid XML, then nothing will have been written to the output range when the exception is thrown (whereas if each function were called individually, then the start tag would have been written before the exception was thrown from writeText).

If more control is needed over the formatting, or if attributes are needed on the start tag, then the functions will have to be called separately instead of calling writeTaggedText.

  1. void writeTaggedText(ref XW writer, string name, R text, Newline newline = Newline.yes, InsertIndent insertIndent = InsertIndent.yes)
    void
    writeTaggedText
    (
    XW
    R
    )
    if (
    isInstanceOf!(XMLWriter, XW) &&
    isForwardRange!R
    &&
    isSomeChar!(ElementType!R)
    )
  2. void writeTaggedText(ref XW writer, string name, R text, InsertIndent insertIndent, Newline newline = Newline.yes)

Parameters

writer
Type: XW

The XMLWriter to write to.

name
Type: string

The _name of the start tag.

text
Type: R

The _text to write between the start and end tags.

newline
Type: Newline

Whether a _newline followed by an indent will be written to the output range before the start tag.

insertIndent

Whether an indent will be inserted after each _newline within the _text.

Throws

XMLWritingException if the given _name is an invalid XML tag _name or if the given _text contains any characters or sequence of characters which are not legal in the _text portion of an XML document. dxml.util.encodeText can be used to encode any characters that are not legal in their literal form in the _text but are legal as entity references.

Examples

1 import std.array : appender;
2 
3 {
4     auto writer = xmlWriter(appender!string());
5     writer.writeStartTag("root", Newline.no);
6     writer.writeTaggedText("foo", "Some text between foos");
7     writer.writeEndTag("root");
8 
9     assert(writer.output.data ==
10            "<root>\n" ~
11            "    <foo>Some text between foos</foo>\n" ~
12            "</root>");
13 }
14 
15 // With Newline.no
16 {
17     auto writer = xmlWriter(appender!string());
18     writer.writeStartTag("root", Newline.no);
19     writer.writeTaggedText("foo", "Some text between foos", Newline.no);
20     writer.writeEndTag("root");
21 
22     assert(writer.output.data ==
23            "<root><foo>Some text between foos</foo>\n" ~
24            "</root>");
25 }
26 
27 // With InsertIndent.yes
28 {
29     auto writer = xmlWriter(appender!string());
30     writer.writeStartTag("root", Newline.no);
31     writer.writeTaggedText("foo", "Some text\nNext line");
32     writer.writeEndTag("root");
33 
34     assert(writer.output.data ==
35            "<root>\n" ~
36            "    <foo>Some text\n" ~
37            "        Next line</foo>\n" ~
38            "</root>");
39 }
40 
41 // With InsertIndent.no
42 {
43     auto writer = xmlWriter(appender!string());
44     writer.writeStartTag("root", Newline.no);
45     writer.writeTaggedText("foo", "Some text\nNext line", InsertIndent.no);
46     writer.writeEndTag("root");
47 
48     assert(writer.output.data ==
49            "<root>\n" ~
50            "    <foo>Some text\n" ~
51            "Next line</foo>\n" ~
52            "</root>");
53 }

See Also

Meta