#include <XmlWriter.hpp>
Public Member Functions | |
XmlWriter (const std::string &root_element, const std::string &style_file_name="") | |
XmlWriter & | operator<< (const stream_command &command) |
XmlWriter & | operator<< (const element &command) |
XmlWriter & | operator<< (const attribute &command) |
XmlWriter & | operator<< (const comment &command) |
XmlWriter & | operator<< (const std::string &string) |
XmlWriter & | operator<< (const char *str) |
XmlWriter & | operator<< (float_t value) |
XmlWriter & | operator<< (size_t value) |
template<size_t N, class data_t> | |
XmlWriter & | operator<< (const ublas::fixed_vector< data_t, N > &value) |
template<class data_t> | |
XmlWriter & | operator<< (const data_t &object) |
template<class data_t> | |
XmlWriter & | operator<< (const std::vector< data_t > &vector) |
XmlWriter & | operator<< (const std::vector< float_t > &vector) |
template<class data_t> | |
XmlWriter & | operator<< (const std::vector< boost::shared_ptr< data_t > > &vector) |
void | write_file (const std::string &file_name) |
Static Public Attributes | |
static const stream_command | end_element |
Stream command to end an element. | |
Classes | |
class | attribute |
Stream command to add an attribute at the current path. More... | |
class | comment |
Stream command to add a comment at the current path. More... | |
class | element |
Stream command to add an element to the current path. More... |
This class enables you to write an XML file using an interface which loosely resembles the stream interface used in iostream to write to output streams. An XmlWriter object is initialized by a root element and then represents an XML stream to which you can write using operator<<(const data_t &), where data_t is the type of the object to be written. XmlWriter provides the stream operator for the built-in data types. If called for other types, XmlWriter calls the function xml_handler<data_t>::write_object() which then writes the object to the stream. I.e. by specializing xml_handler for custom data types the user enables XmlWriter to write these types. Implementations of xml_handler for various classes of the imaging2 modules can be found in the files xmlio.hpp and xmlio.cxx in the module subdirectories.
An XmlWriter object writes objects at the position of its current path. This path is the sequence of element names (and possibly indices) which leads to a given node in an XML file. This concept is very similiar to the idea of XPath. The current path of a XmlWriter can be altered by passing stream commands to the input stream. An example is given below:
using imaging; XmlWriter xml_out("imaging2"); // construct an output stream; "imaging2" is the root element std::vector<float_t> some_floats; // a vector of floats xml_out << XmlWriter::element("some_float"); // set the current element to "some_float" xml_out << some_floats; // write the floats, each of them is enclosed by <some_float>...</some_float> // there is no need to pass XmlWriter::end_element here, because float is a built-in type std::string some_name("Franz"); unsigned int some_age(50); xml_out << XmlWriter::element("person"); // add element "person" to the current path xml_out << XmlWriter::attribute("age") << some_age; // write attribute "age" xml_out << some_person; // write person's name xml_out << XmlWriter::end_element; // close element "person" xml_out.write_file("file.xml"); // write stream content to file
<imaging2>
<person age="50">Franz</person>
<some_float>...</some_float>
<some_float>...</some_float>
</imaging2>
Assume further that the user has implemented a class Person and xml_handler<Person> which writes a person's data as above. Assuming that the class declaration and the declaration of xml_handler<Person> both reside in "Person.h", then she can use XmlWriter in the following way:
#include "Person.h" XmlWriter xml_out("imaging2"); // construct an output stream; "imaging2" is the root element Person person; std::vector<Person> people; // fill person and people with data... xml_out << person; // write person (note that there is no need to add the // element "person" to the current path) xml_out << people; // write all the people at the current path
<person/>
element for the first person and one for each entry of the vector people.
Note writing a vector of floats is different from a custom data type. For the custom data Person neither XmlWriter::element() nor XmlWriter::end_element have to be passed to the stream. In case of a vector of floats XmlWriter::element() must be passed, but not XmlWriter::end_element. The reason for this behavior is that XmlWriter does not know which element name identifies the floats. Introducing a standard element name (such as <float> </float>
) would often be inconvenient because it makes intuitive element names like <super_parameter>10^9</super_parameter>
impossible. This behavior is the same for all built-in types.
imaging::XmlWriter::XmlWriter | ( | const std::string & | root_element, | |
const std::string & | style_file_name = "" | |||
) |
Constructs an XmlWriter object and adds root_element. If style_file_name contains the address of an XSL file, an xml-stylesheet
tag pointing to the the file is added to the resulting XML file.
XmlWriter & imaging::XmlWriter::operator<< | ( | const stream_command & | command | ) |
Passes any other stream command to the stream.
Passes an XmlReader::element() command to the stream.
Passes an XmlReader::attribute() command to the stream.
Writes a comment to the stream.
XmlWriter & imaging::XmlWriter::operator<< | ( | const std::string & | string | ) |
Writes a string to the stream.
XmlWriter & imaging::XmlWriter::operator<< | ( | const char * | str | ) |
Writes a character to the stream.
Writes a floating point value to the stream.
Writes a unsigned integer to the stream.
XmlWriter& imaging::XmlWriter::operator<< | ( | const ublas::fixed_vector< data_t, N > & | value | ) | [inline] |
Writes a vector of tla::vector objects to the stream.
XmlWriter& imaging::XmlWriter::operator<< | ( | const data_t & | object | ) | [inline] |
Writes a data_t object to the stream. The class specialization xml_handler<data_t>::write_object() has to be defined.
References end_element.
XmlWriter& imaging::XmlWriter::operator<< | ( | const std::vector< data_t > & | vector | ) | [inline] |
Writes a vector of data_t objects to the stream. The class specialization xml_handler<data_t>::write_object() has to be defined.
Writes a vector of floats objects to the stream. Each float is enclosed by element tags defined by the last XmlWriter::element() passed to the input stream. In this respect this function is different from the generic version operator<<(data_t &).
XmlWriter& imaging::XmlWriter::operator<< | ( | const std::vector< boost::shared_ptr< data_t > > & | vector | ) | [inline] |
Writes a vector of data_t objects to the stream. The class specialization xml_handler<data_t>::write_object() has to be defined.
void imaging::XmlWriter::write_file | ( | const std::string & | file_name | ) |
Writes the content of the stream to file_name.
References imaging::MessageInterface::DEBUG_ONLY, and imaging::MessageInterface::out.