vsl_basic_xml_element.cxx
Go to the documentation of this file.
1 // This is core/vsl/vsl_basic_xml_element.cxx
3 //:
4 // \file
5 
6 void vsl_basic_xml_element::add_attribute_list(std::vector<std::pair<std::string, std::string> > attrs)
7 {
8  for (const auto & attr : attrs) {
9  attrs_.push_back(attr);
10  }
11 }
12 
13 void vsl_basic_xml_element::add_attribute(std::string attr_name, std::string value)
14 {
15  std::pair<std::string, std::string> attr(attr_name, value);
16  attrs_.push_back(attr);
17 }
18 
19 void vsl_basic_xml_element::add_attribute(std::string attr_name, double value)
20 {
21  std::string value_str = toString(value);
22  std::pair<std::string, std::string> attr(attr_name, value_str.data());
23  attrs_.push_back(attr);
24 }
25 
26 void vsl_basic_xml_element::add_attribute(std::string attr_name, long value)
27 {
28  std::string value_str = toString(value);
29  std::pair<std::string, std::string> attr(attr_name, value_str);
30  attrs_.push_back(attr);
31 }
32 
33 void vsl_basic_xml_element::append_cdata(const std::string& cdata)
34 {
35  if (cdata_.size() > 0)
36  cdata_.append(" ");
37  cdata_.append(cdata);
38 }
39 
41 {
42  if (cdata_.size() > 0)
43  cdata_.append(" ");
44  cdata_.append(toString(cdata));
45 }
46 
48 {
49  if (cdata_.size() > 0)
50  cdata_.append(" ");
51  cdata_.append(toString(cdata));
52 }
53 
54 #if 0
55 bool vsl_basic_xml_element::delete_attribute(std::string /*attr_name*/)
56 {
57  std::cerr << "vsl_basic_xml_element::delete_attribute() not yet implemented\n";
58  return false;
59 }
60 #endif
61 
62 void vsl_basic_xml_element::x_write(std::ostream& ostr)
63 {
64  // put the initial bracket with element name and the attribute-value list
65  x_write_open(ostr);
66 
67  // put the character data between the tags
68  if (cdata_.size() > 0)
69  ostr << cdata_ << '\n';
70 
71  // close the element
72  x_write_close(ostr);
73 }
74 
75 void vsl_basic_xml_element::x_write_open(std::ostream& ostr)
76 {
77  ostr << '<' << tag_;
78  for (auto & attr : attrs_) {
79  ostr << ' ' << attr.first << "=\"" << attr.second << '"';
80  }
81  ostr << ">\n";
82 }
83 
84 //: writes the closing tag to the stream
85 void vsl_basic_xml_element::x_write_close(std::ostream& ostr)
86 {
87  ostr << "</" << tag_ << ">\n";
88 }
void add_attribute(std::string attr_name, std::string value)
overloaded methods to add attribute values.
std::vector< std::pair< std::string, std::string > > attrs_
the node attributes
void x_write_open(std::ostream &ostr)
writes the opening tag for this node to the stream.
void append_cdata(const std::string &cdata)
std::string tag_
the node name
creates basic xml nodes and writes them out to the stream A basic node contains only text content,...
void x_write(std::ostream &ostr)
std::string cdata_
the text() content of this node
void x_write_close(std::ostream &ostr)
writes the closing tag for this node to the stream.
void add_attribute_list(std::vector< std::pair< std::string, std::string > > attrs)
std::string toString(const T &t)