RapidXML.h
Go to the documentation of this file.
1 /****
2  * RapidXML.h - Helper code for using Rapid XML, with some similarity to ArduinoJson API
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the RapidXML Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <WString.h>
23 #include <Print.h>
24 #include <debug_progmem.h>
25 
26 #define RAPIDXML_NO_EXCEPTIONS
27 #define RAPIDXML_NO_STREAMS
28 
29 DECLARE_FSTR(FS_xmlns_xml);
30 DECLARE_FSTR(FS_xmlns_xmlns);
31 
32 #include "../../rapidxml/rapidxml.hpp"
33 
34 namespace XML
35 {
36 using Document = rapidxml::xml_document<char>;
37 using Node = rapidxml::xml_node<char>;
38 using NodeType = rapidxml::node_type;
39 using Attribute = rapidxml::xml_attribute<char>;
40 
48 bool deserialize(Document& doc, char* content);
49 
50 static inline bool deserialize(Document& doc, String& content)
51 {
52  return deserialize(doc, content.begin());
53 }
54 
55 bool deserialize(Document& doc, const FlashString& content);
63 size_t serialize(const Node& node, String& buffer, bool pretty = false);
64 
65 String serialize(const Node& node, bool pretty = false);
66 
67 size_t serialize(const Node& node, Print& out, bool pretty = false);
68 
69 static inline size_t serialize(const Node& node, Print* out, bool pretty = false)
70 {
71  return (out == nullptr) ? 0 : serialize(node, *out, pretty);
72 }
82 
93 Node* appendNode(Node* parent, const char* name, const char* value = nullptr, size_t name_size = 0,
94  size_t value_size = 0);
95 
96 static inline Node* appendNode(Node* parent, const String& name, const String& value = nullptr)
97 {
98  return appendNode(parent, name.c_str(), value ? value.c_str() : nullptr, name.length(), value.length());
99 }
100 
101 Node* appendNode(Node* parent, const String& name, const FlashString& value);
102 
103 template <typename TString, typename TValue> Node* appendNode(Node* parent, const TString& name, const TValue& value)
104 {
105  return appendNode(parent, String(name), String(value));
106 }
117 Attribute* appendAttribute(Node* node, const char* name, const char* value, size_t name_size = 0,
118  size_t value_size = 0);
119 
120 static inline Attribute* appendAttribute(Node* node, const String& name, const String& value)
121 {
122  return appendAttribute(node, name.c_str(), value ? value.c_str() : nullptr, name.length(), value.length());
123 }
124 
125 template <typename TString, typename TValue>
126 Attribute* appendAttribute(Node* node, const TString& name, const TValue& value)
127 {
128  return appendAttribute(node, String(name), String(value));
129 }
143 Node* getNode(Node* node, const char* path, const char* ns, size_t ns_len = 0);
144 
145 inline Node* getNode(Node* node, const String& path, const String& ns = nullptr)
146 {
147  return getNode(node, path.c_str(), ns ? ns.c_str() : nullptr, ns.length());
148 }
149 
174 Node* getNode(const Document& doc, const char* path, const char* ns = nullptr, size_t ns_len = 0);
175 
176 inline Node* getNode(const Document& doc, const String& path, const String& ns = nullptr)
177 {
178  return getNode(doc, path.c_str(), ns ? ns.c_str() : nullptr, ns.length());
179 }
190 String getValue(const Node* node, const char* name, size_t name_size, const char* ns = nullptr, size_t ns_size = 0);
191 
192 inline String getValue(const Node* node, const char* name, const char* ns = nullptr)
193 {
194  return name ? getValue(node, name, strlen(name), ns, ns ? strlen(ns) : 0) : nullptr;
195 }
196 
197 inline String getValue(const Node* node, const String& name, const String& ns = nullptr)
198 {
199  return getValue(node, name.c_str(), name.length(), ns ? ns.c_str() : nullptr, ns.length());
200 }
211 String getAttribute(const Node* node, const char* name, size_t name_size);
212 
213 inline String getAttribute(const Node* node, const char* name)
214 {
215  return name ? getAttribute(node, name, strlen(name)) : nullptr;
216 }
217 
218 inline String getAttribute(const Node* node, const String& name)
219 {
220  return getAttribute(node, name.c_str(), name.length());
221 }
224 }; // namespace XML
Node * getNode(Node *node, const char *path, const char *ns, size_t ns_len=0)
rapidxml::xml_attribute< char > Attribute
Definition: RapidXML.h:39
Node * insertDeclaration(Document &doc)
Add a declaration to the document if there isn't one already.
describes a counted string stored in flash memory
Definition: String.hpp:173
bool deserialize(Document &doc, char *content)
De-serialise XML text into a document.
rapidxml::xml_node< char > Node
Definition: RapidXML.h:37
The String class.
Definition: WString.h:136
Definition: RapidXML.h:34
String getValue(const Node *node, const char *name, size_t name_size, const char *ns=nullptr, size_t ns_size=0)
String getAttribute(const Node *node, const char *name, size_t name_size)
rapidxml::node_type NodeType
Definition: RapidXML.h:38
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
size_t serialize(const Node &node, String &buffer, bool pretty=false)
Serialize XML text and append to string buffer.
Provides formatted output to stream.
Definition: Print.h:36
Attribute * appendAttribute(Node *node, const char *name, const char *value, size_t name_size=0, size_t value_size=0)
Append an attribute.
Node * appendNode(Node *parent, const char *name, const char *value=nullptr, size_t name_size=0, size_t value_size=0)
Append a child element with optional value.
DECLARE_FSTR(FS_xmlns_xml)
rapidxml::xml_document< char > Document
Definition: RapidXML.h:36
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243