xml_parser.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // ----------------------------------------------------------------------------
  2. // Copyright (C) 2002-2006 Marcin Kalicinski
  3. // Copyright (C) 2009 Sebastian Redl
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see www.boost.org
  10. // ----------------------------------------------------------------------------
  11. #ifndef BOOST_PROPERTY_TREE_XML_PARSER_HPP_INCLUDED
  12. #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>
  18. #include <boost/property_tree/detail/xml_parser_read_rapidxml.hpp>
  19. #include <fstream>
  20. #include <string>
  21. #include <locale>
  22. namespace boost { namespace property_tree { namespace xml_parser
  23. {
  24. /**
  25. * Reads XML from an input stream and translates it to property tree.
  26. * @note Clears existing contents of property tree. In case of error the
  27. * property tree unmodified.
  28. * @note XML attributes are placed under keys named @c \<xmlattr\>.
  29. * @throw xml_parser_error In case of error deserializing the property tree.
  30. * @param stream Stream from which to read in the property tree.
  31. * @param[out] pt The property tree to populate.
  32. * @param flags Flags controlling the behaviour of the parser.
  33. * The following flags are supported:
  34. * @li @c no_concat_text -- Prevents concatenation of text nodes into
  35. * datastring of property tree. Puts them in
  36. * separate @c \<xmltext\> strings instead.
  37. * @li @c no_comments -- Skip XML comments.
  38. * @li @c trim_whitespace -- Trim leading and trailing whitespace from text,
  39. * and collapse sequences of whitespace.
  40. */
  41. template<class Ptree>
  42. void read_xml(std::basic_istream<
  43. typename Ptree::key_type::value_type
  44. > &stream,
  45. Ptree &pt,
  46. int flags = 0)
  47. {
  48. read_xml_internal(stream, pt, flags, std::string());
  49. }
  50. /**
  51. * Reads XML from a file using the given locale and translates it to
  52. * property tree.
  53. * @note Clears existing contents of property tree. In case of error the
  54. * property tree unmodified.
  55. * @note XML attributes are placed under keys named @c \<xmlattr\>.
  56. * @throw xml_parser_error In case of error deserializing the property tree.
  57. * @param filename The file from which to read in the property tree.
  58. * @param[out] pt The property tree to populate.
  59. * @param flags Flags controlling the bahviour of the parser.
  60. * The following flags are supported:
  61. * @li @c no_concat_text -- Prevents concatenation of text nodes into
  62. * datastring of property tree. Puts them in
  63. * separate @c \<xmltext\> strings instead.
  64. * @li @c no_comments -- Skip XML comments.
  65. * @param loc The locale to use when reading in the file contents.
  66. */
  67. template<class Ptree>
  68. void read_xml(const std::string &filename,
  69. Ptree &pt,
  70. int flags = 0,
  71. const std::locale &loc = std::locale())
  72. {
  73. BOOST_ASSERT(validate_flags(flags));
  74. std::basic_ifstream<typename Ptree::key_type::value_type>
  75. stream(filename.c_str());
  76. if (!stream)
  77. BOOST_PROPERTY_TREE_THROW(xml_parser_error(
  78. "cannot open file", filename, 0));
  79. stream.imbue(loc);
  80. read_xml_internal(stream, pt, flags, filename);
  81. }
  82. /**
  83. * Translates the property tree to XML and writes it the given output
  84. * stream.
  85. * @throw xml_parser_error In case of error translating the property tree to
  86. * XML or writing to the output stream.
  87. * @param stream The stream to which to write the XML representation of the
  88. * property tree.
  89. * @param pt The property tree to tranlsate to XML and output.
  90. * @param settings The settings to use when writing out the property tree as
  91. * XML.
  92. */
  93. template<class Ptree>
  94. void write_xml(std::basic_ostream<
  95. typename Ptree::key_type::value_type
  96. > &stream,
  97. const Ptree &pt,
  98. const xml_writer_settings<
  99. typename Ptree::key_type
  100. > & settings = xml_writer_settings<
  101. typename Ptree::key_type>() )
  102. {
  103. write_xml_internal(stream, pt, std::string(), settings);
  104. }
  105. /**
  106. * Translates the property tree to XML and writes it the given file.
  107. * @throw xml_parser_error In case of error translating the property tree to
  108. * XML or writing to the output stream.
  109. * @param filename The file to which to write the XML representation of the
  110. * property tree.
  111. * @param pt The property tree to tranlsate to XML and output.
  112. * @param loc The locale to use when writing the output to file.
  113. * @param settings The settings to use when writing out the property tree as
  114. * XML.
  115. */
  116. template<class Ptree>
  117. void write_xml(const std::string &filename,
  118. const Ptree &pt,
  119. const std::locale &loc = std::locale(),
  120. const xml_writer_settings<
  121. typename Ptree::key_type
  122. > & settings = xml_writer_settings<typename Ptree::key_type>())
  123. {
  124. std::basic_ofstream<typename Ptree::key_type::value_type>
  125. stream(filename.c_str());
  126. if (!stream)
  127. BOOST_PROPERTY_TREE_THROW(xml_parser_error(
  128. "cannot open file", filename, 0));
  129. stream.imbue(loc);
  130. write_xml_internal(stream, pt, filename, settings);
  131. }
  132. } } }
  133. namespace boost { namespace property_tree
  134. {
  135. using xml_parser::read_xml;
  136. using xml_parser::write_xml;
  137. using xml_parser::xml_parser_error;
  138. using xml_parser::xml_writer_settings;
  139. using xml_parser::xml_writer_make_settings;
  140. } }
  141. #endif