🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
XMLNode.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/GenericValue.hpp"
25#include "AstUtil/ValueView.hpp"
26#include <memory>
27#include <string>
28#include <vector>
29#include <map>
30#include <cstdio>
31
32AST_NAMESPACE_BEGIN
33
39enum class EXMLNodeType
40{
41 eElement,
42 eText,
43 eComment,
44 eCDATA,
45 eDocType,
46};
47
48class XMLNode;
49using HXMLNode = std::unique_ptr<XMLNode>;
50
52class AST_UTIL_API XMLNode
53{
54public:
55 XMLNode() = default;
56
57 XMLNode(EXMLNodeType kind, StringView nameOrText);
58
59 explicit XMLNode(StringView nameOrText);
60
61 ~XMLNode();
62
65 errc_t load(StringView filepath);
66
69 errc_t save(StringView filepath);
70
71
76 EXMLNodeType getKind() const { return kind_; }
77
80 const std::string& getName() const { return nameOrText_; }
81
84 void setName(StringView name);
85
88 const std::string& getText() const { return nameOrText_; }
89
92 void setText(StringView text);
93
96 void addChild(HXMLNode&& child);
97
100 void addText(StringView text);
101
104 void addComment(StringView text);
105
112 // XMLNode* getChild(StringView name, size_t startIndex = 0) const;
113
116 const std::vector<HXMLNode>& getChildren() const { return children_; }
117
118
122 void addAttribute(const std::string& name, const GenericValue& value);
123
124
127 const std::map<std::string, GenericValue>& getAttributes() const { return attributes_; }
128
129
133 ValueView getAttribute(const std::string& name);
134
135
138 void clearChildren();
139
142 void clearAttributes();
143
146 void clear();
147private:
148 A_DISABLE_COPY(XMLNode);
149
154 bool generateXML(FILE* file, int indent) const;
155
159 std::string escapeXML(const std::string& str) const;
160
161private:
162 EXMLNodeType kind_{EXMLNodeType::eElement};
163 std::string nameOrText_{};
164 std::vector<HXMLNode> children_;
165 std::map<std::string, GenericValue> attributes_;
166};
167
170AST_NAMESPACE_END
通用值类
定义 GenericValue.hpp:34
值视图类
定义 ValueView.hpp:41
XML节点
定义 XMLNode.hpp:53
const std::string & getText() const
获取节点文本
定义 XMLNode.hpp:88
const std::vector< HXMLNode > & getChildren() const
获取子节点
定义 XMLNode.hpp:116
const std::map< std::string, GenericValue > & getAttributes() const
获取属性列表
定义 XMLNode.hpp:127
EXMLNodeType getKind() const
获取节点类型
定义 XMLNode.hpp:76
const std::string & getName() const
获取节点名称
定义 XMLNode.hpp:80
EXMLNodeType
定义 XMLNode.hpp:40
@ eElement
元素节点
@ eCDATA
CDATA节点
@ eText
文本节点
@ eComment
注释节点
@ eDocType
文档类型节点