1 #ifndef ETREE_ELEMENT_H 2 #define ETREE_ELEMENT_H 16 #if __cplusplus >= 201103L 17 # include <initializer_list> 20 # define ETREE_EXPLICIT explicit 22 # define ETREE_EXPLICIT 31 struct _xmlXPathCompExpr;
32 struct _xmlXPathContext;
52 typedef std::pair<string, string> kv_pair;
53 typedef std::pair<string, string> ns_pair;
54 typedef std::initializer_list<kv_pair> kv_list;
65 Element
SubElement(Element &parent,
const QName &qname);
79 Element
SubElement(Element &parent,
const QName &qname, kv_list attribs);
110 string tostring(
const ElementTree &e);
118 ElementTree
parse(std::istream &is);
126 ElementTree
parse(
const string &path);
134 ElementTree
parse(
int fd);
166 std::string
tostring(
const Element &e);
174 ElementTree
parse(std::istream &is);
182 ElementTree
parse(
const std::string &path);
190 ElementTree
parse(
int fd);
201 std::ostream &operator<< (std::ostream &out,
const ElementTree &elem);
210 std::ostream &operator<< (std::ostream &out,
const Element &elem);
219 std::ostream &operator<< (std::ostream &out,
const QName &qname);
244 unsigned char val_[
sizeof(T)];
294 bool operator==(
const T &other)
const;
304 ETREE_EXPLICIT
operator bool()
const;
319 const T &operator *()
const;
324 const T *operator ->()
const;
356 QName(
const string &ns,
const string &tag);
370 QName(
const string &qname);
377 QName(
const char *qname);
389 const string &tag()
const;
394 const string &ns()
const;
404 bool equals(
const char *ns,
const char *tag)
const;
412 bool operator==(
const QName &other)
const;
420 bool operator!=(
const QName &other)
const;
428 typedef std::vector<std::pair<std::string, std::string>>
ns_list;
437 _xmlXPathContext *context_;
458 _xmlXPathCompExpr *expr_;
474 XPath(
const char *s);
481 XPath(
const string &s);
503 const string &expr()
const;
526 vector<Element> findall(
const Element &e)
const;
532 vector<Element> removeall(
Element &expr)
const;
544 string findtext(
const Element &e,
const string &default_=
"")
const;
576 string value()
const;
630 bool has(
const QName &qname)
const;
640 string get(
const QName &qname,
const string &default_=
"")
const;
650 void set(
const QName &qname,
const string &s);
659 void set(kv_list attribs);
665 vector<QName> keys()
const;
670 bool remove(
const QName &qname);
686 template<
typename P,
typename T>
687 friend P nodeFor__(
const T &);
723 template<
typename P,
typename T>
724 friend P nodeFor__(
const T &);
786 void ensurens(
const string &uri);
796 void qname(
const QName &qname);
806 void tag(
const string &tag);
817 void ns(
const string &ns);
831 string get(
const QName &qname,
const string &default_=
"")
const;
847 bool operator==(
const Element &other)
const;
853 bool operator!=(
const Element &other)
const;
896 vector<Element> children(
const QName &qn)
const;
903 vector<Element> children()
const;
923 string findtext(
const XPath &expr,
const string &default_=
"")
const;
931 vector<Element> findall(
const XPath &expr)
const;
937 vector<Element> removeall(
const XPath &expr);
969 void insert(
size_t i,
Element &e);
1024 bool ancestorOf(
const Element &e)
const;
1057 string text()
const;
1064 void text(
const string &s);
1078 string tail()
const;
1085 void tail(
const string &s);
1131 template<
typename Function>
1136 for(
auto &child : elem.
children()) {
1142 #define EXCEPTION(name) \ 1143 struct name : public std::runtime_error { \ 1144 name() : std::runtime_error("etree::"#name) {} \ 1147 EXCEPTION(cyclical_tree_error)
1148 EXCEPTION(internal_error)
1149 EXCEPTION(invalid_xpath_error)
1150 EXCEPTION(memory_error)
1151 EXCEPTION(missing_namespace_error)
1152 EXCEPTION(missing_value_error)
1153 EXCEPTION(out_of_bounds_error)
1154 EXCEPTION(parse_error)
1155 EXCEPTION(qname_error)
1156 EXCEPTION(serialization_error)
1167 : std::runtime_error(s) {}
Element SubElement(Element &parent, const QName &qname)
Construct a new child element.
Definition: element.cpp:1808
ElementTree parse(std::istream &is)
Parse an XML document from a STL istream and return it.
Definition: element.cpp:1931
Thrown to indicate libxml2 raised a parse error.
Definition: element.hpp:1164
ElementTree parse(std::istream &is)
Parse an HTML document from a STL istream and return it.
Definition: element.cpp:1985
Represents a mapping of an Element's attribute names to their values.
Definition: element.hpp:609
Manages a set of registered XPath namespaces and extension functions.
Definition: element.hpp:436
Represents a reference to a single XML element.
Definition: element.hpp:721
Manages a compiled XPath expression.
Definition: element.hpp:453
Definition: element.cpp:2046
vector< Element > children(const QName &qn) const
Return children matching a name.
Definition: element.cpp:1436
std::vector< std::pair< std::string, std::string > > ns_list
Represent a list of namespaces and their associated prefixes that should be defined while executing a...
Definition: element.hpp:428
ElementTree namespace; public classes and functions are defined here.
Definition: element.cpp:28
std::string tostring(const Element &e)
Serialize an HTML element.
Definition: element.cpp:1771
Canonical representation for a name-namespace pair, without namespace prefix, in James Clark's Univer...
Definition: element.hpp:342
Represents iteration position produced by AttrMap::begin() and AttrMap::end().
Definition: element.hpp:584
Element fromstring(const char *s)
Parse an HTML document from a character array and return a reference to its root node.
Definition: element.cpp:1967
Element fromstring(const char *s, size_t n)
Parse an XML document from a character array and return a reference to its root node.
Definition: element.cpp:1919
Nullable< Element > NullableElement
Convenient alias for Nullable<Element> to avoid typing "etree::" twice in scopes that do not import N...
Definition: element.hpp:331
void visit(Element elem, Function func)
Depth-first visit an element and all of its subelements.
Definition: element.hpp:1133
Proxy value type yielded by AttrIterator.
Definition: element.hpp:551
Lightweight wrapper to add nullable semantics to another type.
Definition: element.hpp:240
string tostring(const Element &e)
Serialize an element.
Definition: element.cpp:1771
Represents iteration position produced by Element::begin() and Element::end().
Definition: element.hpp:1103
Represents a reference to the root of an XML tree, the document itself.
Definition: element.hpp:684