1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#pragma once
#include <pugixml.hpp>
#include <string>
#include <vector>
namespace xml
{
// Typedefs
class Node;
typedef std::vector<Node> NodeList;
class Document;
/* Node
*
* A representation of an XML node. This class wraps a pugixml node
* and provides certain methods to access properties of the node.
*/
class Node
{
const Document* _owner = nullptr;
// The contained pugi::xml_node. This points to part of a wider document
// structure which is not owned by this Node object.
pugi::xml_node _xmlNode;
public:
/// Construct a Node with no contained data
Node(const Document* owner = nullptr): _owner(owner)
{}
/// Construct a Node from a provided pugi::xml_node
Node(const Document* owner, pugi::xml_node node): _owner(owner), _xmlNode(node)
{}
Node(const Node& other) = default;
Node(Node&& other) = default;
Node& operator=(const Node& other) = default;
Node& operator=(Node&& other) = default;
// Returns true if this node is not empty (null)
bool isValid() const;
// Get the name of the given node
std::string getName() const;
// Get a list of nodes which are children of this node
NodeList getChildren() const;
// Creates a new child under this XML Node
Node createChild(const std::string& name);
// Get a list of nodes which are children of this node and match the
// given name.
NodeList getNamedChildren(const std::string& name) const;
// Return the value of the given attribute, or an empty string
// if the attribute is not present on this Node.
std::string getAttributeValue(const std::string& key) const;
// Set the value of the given attribute
void setAttributeValue(const std::string& key, const std::string& value);
/// Remove an attribute altogether
void removeAttribute(const std::string& key);
/// Return the text content of this node.
std::string getContent() const;
// Sets the contents of this XML node to the given string
void setContent(const std::string& content);
void addText(const std::string& text);
// Unlink and delete the node and all its children
void erase();
private:
friend class Document;
// Private accessor, used by the friend Document class
pugi::xml_node getNodePtr() const;
};
} // namespace xml
|