fix doc example typo
[boost.git] / boost / property_tree / xml_parser.hpp
blobdea939da541f3e181108d6e9e45d1b61e4c97764
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_XML_PARSER_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_XML_PARSER_HPP_INCLUDED
13 #include <boost/property_tree/ptree.hpp>
14 #include <boost/property_tree/detail/xml_parser_write.hpp>
15 #include <boost/property_tree/detail/xml_parser_error.hpp>
16 #include <boost/property_tree/detail/xml_parser_writer_settings.hpp>
17 #include <boost/property_tree/detail/xml_parser_flags.hpp>
19 // Include proper parser
20 #if defined(BOOST_PROPERTY_TREE_XML_PARSER_TINYXML)
21 #include <boost/property_tree/detail/xml_parser_read_tinyxml.hpp>
22 #elif defined(BOOST_PROPERTY_TREE_XML_PARSER_PUGXML)
23 #include <boost/property_tree/detail/xml_parser_read_pugxml.hpp>
24 #elif defined(BOOST_PROPERTY_TREE_XML_PARSER_PUGIXML)
25 #include <boost/property_tree/detail/xml_parser_read_pugixml.hpp>
26 #elif defined(BOOST_PROPERTY_TREE_XML_PARSER_SPIRIT)
27 #include <boost/property_tree/detail/xml_parser_read_spirit.hpp>
28 #else
29 #include <boost/property_tree/detail/xml_parser_read_rapidxml.hpp>
30 #endif
32 #include <fstream>
33 #include <string>
34 #include <locale>
36 namespace boost { namespace property_tree { namespace xml_parser
39 /**
40 * Reads XML from an input stream and translates it to property tree.
41 * @note Clears existing contents of property tree. In case of error the property tree unmodified.
42 * @note XML attributes are placed under keys named @c \<xmlattr\>.
43 * @throw xml_parser_error In case of error deserializing the property tree.
44 * @param stream Stream from which to read in the property tree.
45 * @param[out] pt The property tree to populate.
46 * @param flags Flags controlling the bahviour of the parser.
47 * The following flags are supported:
48 * @li @c no_concat_text -- Prevents concatenation of text nodes into datastring
49 * of property tree. Puts them in separate @c \<xmltext\>
50 * strings instead.
51 * @li @c no_comments -- Skip XML comments.
53 template<class Ptree>
54 void read_xml(std::basic_istream<typename Ptree::key_type::value_type> &stream,
55 Ptree &pt,
56 int flags = 0)
58 read_xml_internal(stream, pt, flags, std::string());
61 /**
62 * Reads XML from a file using the given locale and translates it to property tree.
63 * @note Clears existing contents of property tree. In case of error the property tree unmodified.
64 * @note XML attributes are placed under keys named @c \<xmlattr\>.
65 * @throw xml_parser_error In case of error deserializing the property tree.
66 * @param filename The file from which to read in the property tree.
67 * @param[out] pt The property tree to populate.
68 * @param flags Flags controlling the bahviour of the parser.
69 * The following flags are supported:
70 * @li @c no_concat_text -- Prevents concatenation of text nodes into datastring
71 * of property tree. Puts them in separate @c \<xmltext\>
72 * strings instead.
73 * @li @c no_comments -- Skip XML comments.
74 * @param loc The locale to use when reading in the file contents.
76 template<class Ptree>
77 void read_xml(const std::string &filename,
78 Ptree &pt,
79 int flags = 0,
80 const std::locale &loc = std::locale())
82 BOOST_ASSERT(validate_flags(flags));
83 std::basic_ifstream<typename Ptree::key_type::value_type> stream(filename.c_str());
84 if (!stream)
85 BOOST_PROPERTY_TREE_THROW(xml_parser_error("cannot open file", filename, 0));
86 stream.imbue(loc);
87 read_xml_internal(stream, pt, flags, filename);
90 /**
91 * Translates the property tree to XML and writes it the given output stream.
92 * @throw xml_parser_error In case of error translating the property tree to XML
93 * or writing to the output stream.
94 * @param stream The stream to which to write the XML representation of the
95 * property tree.
96 * @param pt The property tree to tranlsate to XML and output.
97 * @param settings The settings to use when writing out the property tree as XML.
99 template<class Ptree>
100 void write_xml(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
101 const Ptree &pt,
102 const xml_writer_settings<typename Ptree::key_type::value_type> & settings = xml_writer_settings<typename Ptree::key_type::value_type>() )
104 write_xml_internal(stream, pt, std::string(), settings);
108 * Translates the property tree to XML and writes it the given file.
109 * @throw xml_parser_error In case of error translating the property tree to XML
110 * or writing to the output stream.
111 * @param filename The file to which to write the XML representation of the
112 * property tree.
113 * @param pt The property tree to tranlsate to XML and output.
114 * @param loc The locale to use when writing the output to file.
115 * @param settings The settings to use when writing out the property tree as XML.
117 template<class Ptree>
118 void write_xml(const std::string &filename,
119 const Ptree &pt,
120 const std::locale &loc = std::locale(),
121 const xml_writer_settings<typename Ptree::key_type::value_type> & settings = xml_writer_settings<typename Ptree::key_type::value_type>())
123 std::basic_ofstream<typename Ptree::key_type::value_type> stream(filename.c_str());
124 if (!stream)
125 BOOST_PROPERTY_TREE_THROW(xml_parser_error("cannot open file", filename, 0));
126 stream.imbue(loc);
127 write_xml_internal(stream, pt, filename, settings);
130 } } }
132 namespace boost { namespace property_tree
134 using xml_parser::read_xml;
135 using xml_parser::write_xml;
136 using xml_parser::xml_parser_error;
138 using xml_parser::xml_writer_settings;
139 using xml_parser::xml_writer_make_settings;
142 #endif