fix doc example typo
[boost.git] / boost / property_tree / detail / xml_parser_read_pugxml.hpp
blob075b9d4c7996661ced9a6673daab0cddc9c7aeab
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_DETAIL_XML_PARSER_READ_PUGXML_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_DETAIL_XML_PARSER_READ_PUGXML_HPP_INCLUDED
13 #include <boost/property_tree/ptree.hpp>
14 #include <boost/property_tree/detail/xml_parser_error.hpp>
15 #include <boost/property_tree/detail/xml_parser_flags.hpp>
16 #include <boost/property_tree/detail/xml_parser_utils.hpp>
17 #include <boost/property_tree/detail/pugxml.hpp>
19 namespace boost { namespace property_tree { namespace xml_parser
22 template<class Ptree>
23 void read_xml_node(pug::xml_node node, Ptree &pt, int flags)
25 typedef typename Ptree::key_type::value_type Ch;
26 if (node.type() == pug::node_element ||
27 node.type() == pug::node_document)
29 Ptree &tmp = pt.push_back(std::make_pair(node.name(), empty_ptree<Ptree>()))->second;
30 for (pug::xml_node::attribute_iterator it = node.attributes_begin(); it != node.attributes_end(); ++it)
31 tmp.put(xmlattr<Ch>() + Ch('.') + it->name(), it->value());
32 for (pug::xml_node::child_iterator it = node.children_begin(); it != node.children_end(); ++it)
33 read_xml_node(*it, tmp, flags);
35 else if (node.type() == pug::node_pcdata)
37 if (flags & no_concat_text)
38 pt.push_back(std::make_pair(xmltext<Ch>(), Ptree(node.value())));
39 else
40 pt.data() += node.value();
42 else if (node.type() == pug::node_comment)
44 if (!(flags & no_comments))
45 pt.push_back(std::make_pair(xmlcomment<Ch>(), Ptree(node.value())));
49 template<class Ptree>
50 void read_xml_internal(std::basic_istream<typename Ptree::key_type::value_type> &stream,
51 Ptree &pt,
52 int flags,
53 const std::string &filename)
56 typedef typename Ptree::key_type::value_type Ch;
58 // Load data into vector
59 std::vector<Ch> data(std::istreambuf_iterator<Ch>(stream.rdbuf()),
60 std::istreambuf_iterator<Ch>());
61 if (!stream.good())
62 BOOST_PROPERTY_TREE_THROW(xml_parser_error("read error", filename, 0));
63 data.push_back(Ch('\0'));
65 // Parse
66 pug::xml_parser parser;
67 if (parser.parse(&data.front()))
69 Ptree local;
70 pug::xml_node doc = parser.document();
71 for (pug::xml_node::child_iterator it = doc.children_begin(); it != doc.children_end(); ++it)
72 read_xml_node(*it, local, flags);
73 local.swap(pt);
75 else
76 BOOST_PROPERTY_TREE_THROW(xml_parser_error("parse error", filename, 0));
80 } } }
82 #endif