using std::make_pair;
using std::map;
using std::stringstream;
+using std::ostream;
using boost::shared_ptr;
using boost::optional;
_content = c ? "1" : "0";
}
+void
+cxml::Node::add_string_attribute (string name, string value)
+{
+ _attributes.push_back (make_pair (name, value));
+}
+
+void
+cxml::Node::add_bool_attribute (string name, bool value)
+{
+ add_string_attribute (name, value ? "1" : "0");
+}
+
cxml::NodePtr
-cxml::Node::add_string (string name, string content)
+cxml::Node::add_string_child (string name, string content)
{
- NodePtr n = add (name);
+ NodePtr n = add_child (name);
n->set_string_content (content);
return n;
}
cxml::NodePtr
-cxml::Node::add_bool (std::string name, bool content)
+cxml::Node::add_bool_child (std::string name, bool content)
{
- NodePtr n = add (name);
+ NodePtr n = add_child (name);
n->set_bool_content (content);
return n;
}
}
void
+cxml::Document::write_to_stream_formatted (ostream& stream, string coding) const
+{
+ xmlpp::Document doc;
+ write_to_xmlpp_document (doc);
+ doc.write_to_stream_formatted (stream, coding);
+}
+
+string
cxml::Document::write_to_string (std::string coding) const
{
xmlpp::Document doc;
write_to_xmlpp_document (doc);
- doc.write_to_string (coding);
+ return doc.write_to_string (coding);
}
void
#define LIBCXML_CXML_H
#include <string>
+#include <sstream>
#include <list>
#include <stdint.h>
#include <boost/shared_ptr.hpp>
namespace xmlpp {
class Node;
+ class Document;
}
namespace cxml {
}
+ /* Adding attributes */
+
+ void add_string_attribute (std::string name, std::string value);
+ void add_bool_attribute (std::string name, bool value);
+
+ template <class T>
+ void add_number_attribute (std::string name, T value)
+ {
+ std::stringstream u;
+ u.imbue (std::locale::classic ());
+ u << value;
+ add_string_attribute (name, u.str ());
+ }
+
+
/* Short-cuts to add nodes with content */
- NodePtr add_string (std::string name, std::string content);
- NodePtr add_bool (std::string name, bool content);
+ NodePtr add_string_child (std::string name, std::string content);
+ NodePtr add_bool_child (std::string name, bool content);
template <class T>
- NodePtr add_number (std::string name, T content)
+ NodePtr add_number_child (std::string name, T content)
{
- NodePtr n = add (name);
+ NodePtr n = add_child (name);
n->set_number_content (content);
return n;
}
NodeList node_children (std::string) const;
/** Add a child node with a given name */
- NodePtr add (std::string name)
+ NodePtr add_child (std::string name)
{
NodePtr n (new cxml::Node ());
n->set_name (name);
void check_root_name (std::string root_name);
void write_to_file_formatted (boost::filesystem::path) const;
- void write_to_string (std::string) const;
+ void write_to_stream_formatted (std::ostream& stream, std::string coding) const;
+ std::string write_to_string (std::string) const;
private:
void write_to_xmlpp_document (xmlpp::Document &) const;
cxml::Document document;
document.set_name ("A");
- document.add_number<int> ("B", 42);
- document.add_string ("C", "fred");
- document.add_number<double> ("D", 42.9);
- document.add_bool ("E", true);
- document.add_number<int> ("F", 1);
- document.add_number<int> ("F", 2);
- cxml::NodePtr h = document.add ("H");
- h->add_string ("I", "testing");
- h->add_string ("I", "more testing");
- document.add ("J")->add_string ("K", "jim");
+ document.add_number_child<int> ("B", 42);
+ document.add_string_child ("C", "fred");
+ document.add_number_child<double> ("D", 42.9);
+ document.add_bool_child ("E", true);
+ document.add_number_child<int> ("F", 1);
+ document.add_number_child<int> ("F", 2);
+ cxml::NodePtr h = document.add_child ("H");
+ h->add_string_child ("I", "testing");
+ h->add_string_child ("I", "more testing");
+ document.add_child ("J")->add_string_child ("K", "jim");
document.write_to_file_formatted ("build/test/b.xml");