1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
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)
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>
29 #include <boost/property_tree/detail/xml_parser_read_rapidxml.hpp>
36 namespace boost
{ namespace property_tree
{ namespace xml_parser
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\>
51 * @li @c no_comments -- Skip XML comments.
54 void read_xml(std::basic_istream
<typename
Ptree::key_type::value_type
> &stream
,
58 read_xml_internal(stream
, pt
, flags
, std::string());
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\>
73 * @li @c no_comments -- Skip XML comments.
74 * @param loc The locale to use when reading in the file contents.
77 void read_xml(const std::string
&filename
,
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());
85 BOOST_PROPERTY_TREE_THROW(xml_parser_error("cannot open file", filename
, 0));
87 read_xml_internal(stream
, pt
, flags
, filename
);
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
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.
100 void write_xml(std::basic_ostream
<typename
Ptree::key_type::value_type
> &stream
,
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
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
,
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());
125 BOOST_PROPERTY_TREE_THROW(xml_parser_error("cannot open file", filename
, 0));
127 write_xml_internal(stream
, pt
, filename
, settings
);
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
;