libyang  1.0.253
YANG data modeling language library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Xml.cpp
Go to the documentation of this file.
1 
15 #include <iostream>
16 #include <memory>
17 #include <stdexcept>
18 #include <vector>
19 
20 #include "Internal.hpp"
21 #include "Xml.hpp"
22 
23 extern "C" {
24 #include "libyang.h"
25 #include "xml.h"
26 }
27 
28 namespace libyang {
29 
30 Xml_Ns::Xml_Ns(const struct lyxml_ns *ns, S_Deleter deleter):
31  ns((struct lyxml_ns *) ns),
32  deleter(deleter)
33 {}
35 S_Xml_Ns Xml_Ns::next() LY_NEW(ns, next, Xml_Ns);
36 
37 Xml_Attr::Xml_Attr(struct lyxml_attr *attr, S_Deleter deleter):
38  attr(attr),
39  deleter(deleter)
40 {}
42 S_Xml_Attr Xml_Attr::next() LY_NEW(attr, next, Xml_Attr);
43 S_Xml_Ns Xml_Attr::ns() LY_NEW(attr, ns, Xml_Ns);
44 
45 Xml_Elem::Xml_Elem(S_Context context, struct lyxml_elem *elem, S_Deleter deleter):
46  context(context),
47  elem(elem),
48  deleter(deleter)
49 {}
51 S_Xml_Elem Xml_Elem::parent() {return elem->parent ? std::make_shared<Xml_Elem>(context, elem->parent, deleter) : nullptr;}
52 S_Xml_Attr Xml_Elem::attr() LY_NEW(elem, attr, Xml_Attr);
53 S_Xml_Elem Xml_Elem::child() {return elem->child ? std::make_shared<Xml_Elem>(context, elem->child, deleter) : nullptr;}
54 S_Xml_Elem Xml_Elem::next() {return elem->next ? std::make_shared<Xml_Elem>(context, elem->next, deleter) : nullptr;}
55 S_Xml_Elem Xml_Elem::prev() {return elem->prev ? std::make_shared<Xml_Elem>(context, elem->prev, deleter) : nullptr;}
56 S_Xml_Ns Xml_Elem::ns() LY_NEW(elem, ns, Xml_Ns);
57 const char *Xml_Elem::get_attr(const char *name, const char *ns) {
58  return lyxml_get_attr(elem, name, ns);
59 }
60 S_Xml_Ns Xml_Elem::get_ns(const char *prefix) {
61  const struct lyxml_ns *ns = lyxml_get_ns(elem, prefix);
62  return elem->ns ? std::make_shared<Xml_Ns>((struct lyxml_ns *)ns, deleter) : nullptr;
63 }
64 std::string Xml_Elem::print_mem(int options) {
65  char *data = nullptr;
66 
67  lyxml_print_mem(&data, (const struct lyxml_elem *) elem, options);
68  if (!data) {
69  return nullptr;
70  }
71 
72  std::string s_data = data;
73  free(data);
74  return s_data;
75 }
76 
77 std::vector<S_Xml_Elem> Xml_Elem::tree_for() {
78  std::vector<S_Xml_Elem> s_vector;
79 
80  struct lyxml_elem *elem = nullptr;
81  LY_TREE_FOR(elem, elem) {
82  s_vector.push_back(std::make_shared<Xml_Elem>(context, elem, deleter));
83  }
84 
85  return s_vector;
86 }
87 std::vector<S_Xml_Elem> Xml_Elem::tree_dfs() {
88  std::vector<S_Xml_Elem> s_vector;
89 
90  struct lyxml_elem *elem = nullptr, *next = nullptr;
91  LY_TREE_DFS_BEGIN(elem, next, elem) {
92  s_vector.push_back(std::make_shared<Xml_Elem>(context, elem, deleter));
93  LY_TREE_DFS_END(elem, next, elem)
94  }
95 
96  return s_vector;
97 }
98 
99 }
std::vector< S_Xml_Elem > tree_for()
Definition: Xml.cpp:77
struct lyxml_elem * next
Definition: xml.h:100
class for wrapping lyxml_ns.
Definition: Xml.hpp:43
int lyxml_print_mem(char **strp, const struct lyxml_elem *elem, int options)
Dump XML tree to a IO stream.
S_Xml_Attr next()
Definition: Xml.cpp:42
Element&#39;s attribute definition.
Definition: xml.h:75
S_Xml_Ns next()
Definition: Xml.cpp:35
struct lyxml_elem * parent
Definition: xml.h:97
S_Xml_Attr attr()
Definition: Xml.cpp:52
Class implementation for libyang C header xml.h.
struct lyxml_ns * lyxml_get_ns(const struct lyxml_elem *elem, const char *prefix)
Get namespace definition of the given prefix in context of the specified element. ...
struct lyxml_ns * ns
Definition: xml.h:104
Xml_Ns(const struct lyxml_ns *ns, S_Deleter deleter)
Definition: Xml.cpp:30
S_Xml_Ns ns()
Definition: Xml.cpp:56
S_Xml_Elem parent()
Definition: Xml.cpp:51
S_Xml_Ns get_ns(const char *prefix)
Definition: Xml.cpp:60
S_Xml_Elem next()
Definition: Xml.cpp:54
Public API of libyang XML parser.
std::string print_mem(int options)
Definition: Xml.cpp:64
#define LY_TREE_DFS_BEGIN(START, NEXT, ELEM)
Macro to iterate via all elements in a tree. This is the opening part to the LY_TREE_DFS_END - they a...
Definition: tree_schema.h:90
Structure describing an element in an XML tree.
Definition: xml.h:92
#define LY_TREE_DFS_END(START, NEXT, ELEM)
Definition: tree_schema.h:122
struct lyxml_elem * prev
Definition: xml.h:101
S_Xml_Elem child()
Definition: Xml.cpp:53
#define LY_TREE_FOR(START, ELEM)
Macro to iterate via all sibling elements without affecting the list itself.
Definition: tree_schema.h:40
S_Xml_Elem prev()
Definition: Xml.cpp:55
Namespace definition.
Definition: xml.h:58
const char * lyxml_get_attr(const struct lyxml_elem *elem, const char *name, const char *ns)
Get value of the attribute in the specified element.
std::vector< S_Xml_Elem > tree_dfs()
Definition: Xml.cpp:87