Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
xml.hpp
Go to the documentation of this file.
1#pragma once
2
3// XML parse/generate wrapper over pugixml.
4// Provides a simplified API for common XML operations.
5
6#include <string>
7#include <string_view>
8#include <vector>
9#include <optional>
10#include <functional>
11
12// Forward-declare pugixml types to avoid header leak
13namespace pugi { class xml_document; class xml_node; }
14
15namespace pulp::runtime {
16
19public:
22
24 bool parse(std::string_view xml_string);
25
27 bool load_file(std::string_view path);
28
30 std::string to_string() const;
31
33 bool save_file(std::string_view path) const;
34
36 std::string root_name() const;
37
39 std::optional<std::string> root_attribute(std::string_view name) const;
40
43 std::vector<std::string> xpath_strings(std::string_view xpath) const;
44
46 std::optional<std::string> xpath_string(std::string_view xpath) const;
47
49 void walk(std::function<void(std::string_view name, std::string_view text)> fn) const;
50
52 bool is_valid() const { return valid_; }
53
55 const std::string& error() const { return error_; }
56
57 // No copy, move OK
58 XmlDocument(const XmlDocument&) = delete;
61 XmlDocument& operator=(XmlDocument&&) noexcept;
62
63private:
64 struct Impl;
65 Impl* impl_ = nullptr;
66 bool valid_ = false;
67 std::string error_;
68};
69
72std::string xml_generate(std::string_view root_name,
73 const std::vector<std::pair<std::string, std::string>>& elements);
74
75} // namespace pulp::runtime
bool save_file(std::string_view path) const
Save the document to a file. Returns false on failure.
std::optional< std::string > root_attribute(std::string_view name) const
Get an attribute value from the root element.
XmlDocument & operator=(const XmlDocument &)=delete
std::optional< std::string > xpath_string(std::string_view xpath) const
Get text content of the first element matching an XPath expression.
bool is_valid() const
Whether the document was parsed successfully.
Definition xml.hpp:52
std::string to_string() const
Save the document to a string.
void walk(std::function< void(std::string_view name, std::string_view text)> fn) const
Walk all elements, calling fn with (element_name, text_content).
XmlDocument(XmlDocument &&) noexcept
const std::string & error() const
Last parse error message.
Definition xml.hpp:55
XmlDocument(const XmlDocument &)=delete
std::vector< std::string > xpath_strings(std::string_view xpath) const
Find all elements matching an XPath expression.
bool load_file(std::string_view path)
Load XML from a file. Returns false on failure.
std::string root_name() const
Get the root element name.
bool parse(std::string_view xml_string)
Parse XML from a string. Returns false on failure.
Definition xml.hpp:13
Definition generated_event_source.hpp:33
std::string xml_generate(std::string_view root_name, const std::vector< std::pair< std::string, std::string > > &elements)
Generate a simple XML string from key-value pairs.
STL namespace.