PLaSK library

Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
plask::XMLWriter Struct Reference

Object of this class help produce valid XML documents. More...

`#include <plask/utils/xml/writer.hpp>`

## Classes
class | Element
| Represent single XML element connected with writer. More...
struct | Output
| Base class for output (stream). More...

## Public Member Functions
| XMLWriter (std::ostream &out, std::size_t indentation=2)
| Construct XML writer which will write content to given `out` stream.
| XMLWriter (const std::string &file_name, std::size_t indentation=2)
| Construct XML writer which will write content to file with given name.
| XMLWriter (std::FILE *cfile, std::size_t indentation=2)
| Construct XML writer which will write content to given C file.
| XMLWriter (Output *out, std::size_t indentation=2)
| Construct XML writer which will write content to given `out`.
| ~XMLWriter ()
| Delete output object.
void | writeHeader ()
| Append to stream XML document header.
template<typename name_t >
Element | addElement (name_t &&name)
| Append element/tag to stream.
template<typename name_t >
Element | addTag (name_t &&name)
| Append element/tag to stream.
template<typename text_t >
void | writeText (text_t &&text)
| Write text content for the current element.
template<typename text_t >
void | writeCDATA (text_t &&cdata)
| Write CDATA for the current element.
void | indent ()
| Write spaces to the current indentation level.
Element * | getCurrent ()
| Get current element.

## Detailed Description

Object of this class help produce valid XML documents.

It holds std output stream and helps format valid XML data written to this stream.

Example:

XMLWriter w(some_out_stream);
w.writeHeader();
w.addElement("a").attr("x", 1).attr("y", 2); // <a x="1" y="2"/>
{
XMLWriter::Element outer(w, "o"); // or XMLWriter::Element outer = w.addElement("o");
outer.attr("o_attr", "v");
w.element("i").writeText("inner content");
// here outer.attr(...) is not allowed and will throw an exception
} // <o o_attr="v"><i>inner content</i></o>
w.addElement("o").addElement("i1").attr("a", 1).addElement("i2"); // <o><i1 a="1"><i2/></i1></o>
w.addElement("o").addElement("i1").attr("a", 1).end().addElement("i2"); // <o><i1 a="1"/><i2/></o>

Definition at line 46 of file writer.hpp.

## Constructor & Destructor Documentation

## ◆ XMLWriter() [1/4]

plask::XMLWriter::XMLWriter | ( | std::ostream & | out,
|  | std::size_t | indentation = `2`
| ) |  |

Construct XML writer which will write content to given `out` stream.

Parameters

  out | output stream which will be used as destination to XML content, will be not closed by this writer
  indentation | indentation for each tag level

Definition at line 210 of file writer.cpp.

## ◆ XMLWriter() [2/4]

plask::XMLWriter::XMLWriter | ( | const std::string & | file_name,
|  | std::size_t | indentation = `2`
| ) |  |

Construct XML writer which will write content to file with given name.

Parameters

  file_name | name of file which will be used as destination to XML content
  indentation | indentation for each tag level

Definition at line 213 of file writer.cpp.

## ◆ XMLWriter() [3/4]

plask::XMLWriter::XMLWriter | ( | std::FILE * | cfile,
|  | std::size_t | indentation = `2`
| ) |  |

Construct XML writer which will write content to given C file.

Writer will not close given descriptor.

Parameters

  cfile | opened, C file descriptor, writer will not close it
  indentation | indentation for each tag level

Definition at line 217 of file writer.cpp.

## ◆ XMLWriter() [4/4]

plask::XMLWriter::XMLWriter | ( | Output * | out,
|  | std::size_t | indentation = `2`
| ) |  |

Construct XML writer which will write content to given `out`.

Parameters

  out | output which will be used as destination to XML content, will be delete (with `delete` operator) by writer destructor
  indentation | indentation for each tag level

Definition at line 221 of file writer.cpp.

## ◆ ~XMLWriter()

plask::XMLWriter::~XMLWriter | ( |  | ) |
| inline

Delete output object.

Definition at line 324 of file writer.hpp.

## Member Function Documentation

## ◆ addElement()

template<typename name_t >

Element plask::XMLWriter::addElement | ( | name_t && | name | ) |
| inline

Append element/tag to stream.

Parameters

  name | tag name

Returns
  object which allow to set details of element (attributes, content, etc.)

Definition at line 338 of file writer.hpp.

## ◆ addTag()

template<typename name_t >

Element plask::XMLWriter::addTag | ( | name_t && | name | ) |
| inline

Append element/tag to stream.

Parameters

  name | tag name

Returns
  object which allow to set details of element (attributes, content, etc.)

Definition at line 346 of file writer.hpp.

## ◆ getCurrent()

Element * plask::XMLWriter::getCurrent | ( |  | ) |
| inline

Get current element.

Returns
  current element or `nullptr` if there are no elements (root was not open jet or was already closed)

Definition at line 379 of file writer.hpp.

## ◆ indent()

void plask::XMLWriter::indent | ( |  | ) |
| inline

Write spaces to the current indentation level.

Definition at line 371 of file writer.hpp.

## ◆ writeCDATA()

template<typename text_t >

void plask::XMLWriter::writeCDATA | ( | text_t && | cdata | ) |
| inline

Write CDATA for the current element.

Parameters

  cdata | data to write

Definition at line 363 of file writer.hpp.

## ◆ writeHeader()

void plask::XMLWriter::writeHeader | ( |  | ) |
| inline

Append to stream XML document header.

Definition at line 327 of file writer.hpp.

## ◆ writeText()

template<typename text_t >

void plask::XMLWriter::writeText | ( | text_t && | text | ) |
| inline

Write text content for the current element.

Parameters

  text | text to write

Definition at line 353 of file writer.hpp.

The documentation for this struct was generated from the following files:

- plask/utils/xml/writer.hpp
- plask/utils/xml/writer.cpp