Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions include/boost/property_tree/detail/xml_parser_write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,32 @@ namespace boost { namespace property_tree { namespace xml_parser

// Write attributes
if (optional<const Ptree &> attribs = pt.get_child_optional(xmlattr<Str>()))
{
bool attrInSeparateLine = want_pretty && settings.attr_separate_line;
for (It it = attribs.get().begin(); it != attribs.get().end(); ++it)
stream << Ch(' ') << it->first << Ch('=')
<< Ch('"')
<< encode_char_entities(
it->second.template get_value<Str>())
<< Ch('"');
{
if (attrInSeparateLine)
{
stream << Ch('\n');
write_xml_indent(stream,indent+1,settings);
}
else
{
stream << Ch(' ');
}

stream << it->first << Ch('=')
<< Ch('"')
<< encode_char_entities(
it->second.template get_value<Str>())
<< Ch('"');
}
if (attrInSeparateLine)
{
stream << Ch('\n');
write_xml_indent(stream,indent,settings);
}
}

if ( has_attrs_only )
{
Expand Down
16 changes: 10 additions & 6 deletions include/boost/property_tree/detail/xml_parser_writer_settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

namespace boost { namespace property_tree { namespace xml_parser
{

// Naively convert narrow string to another character type
template<class Str>
Str widen(const char *text)
{
typedef typename Str::value_type Ch;
typedef typename Str::value_type Ch;
Str result;
while (*text)
{
Expand All @@ -35,28 +35,32 @@ namespace boost { namespace property_tree { namespace xml_parser
template<class Str>
class xml_writer_settings
{
typedef typename Str::value_type Ch;
typedef typename Str::value_type Ch;
public:
xml_writer_settings(Ch inchar = Ch(' '),
typename Str::size_type incount = 0,
const Str &enc = widen<Str>("utf-8"))
const Str &enc = widen<Str>("utf-8"),
bool attr_separate_line = false)
: indent_char(inchar)
, indent_count(incount)
, encoding(enc)
, attr_separate_line(attr_separate_line)
{
}

Ch indent_char;
typename Str::size_type indent_count;
Str encoding;
bool attr_separate_line;
};

template <class Str>
xml_writer_settings<Str> xml_writer_make_settings(typename Str::value_type indent_char = (typename Str::value_type)(' '),
typename Str::size_type indent_count = 0,
const Str &encoding = widen<Str>("utf-8"))
const Str &encoding = widen<Str>("utf-8"),
bool attr_separate_line = false)
{
return xml_writer_settings<Str>(indent_char, indent_count, encoding);
return xml_writer_settings<Str>(indent_char, indent_count, encoding, attr_separate_line);
}

} } }
Expand Down