Chromium 中libxml使用c++

embedded/2025/1/14 20:35:45/

Chromium中包含libxml 解析库

读取和写文件包含对应头文件即可。

1、读取xml

third_party/libxml/chromium/xml_reader.h


#include <map>
#include <string>extern "C" {
struct _xmlTextReader;
}// XmlReader is a wrapper class around libxml's xmlReader,
// providing a simplified C++ API.
class XmlReader {public:XmlReader();~XmlReader();// Load a document into the reader from memory.  |input| must be UTF-8 and// exist for the lifetime of this object.  Returns false on error.// TODO(evanm): handle encodings other than UTF-8?bool Load(const std::string& input);// Load a document into the reader from a file.  Returns false on error.bool LoadFile(const std::string& file_path);// Wrappers around libxml functions -----------------------------------------// Read() advances to the next node.  Returns false on EOF or error.bool Read();// Next(), when pointing at an opening tag, advances to the node after// the matching closing tag.  Returns false on EOF or error.bool Next();// Return the depth in the tree of the current node.int Depth();// Returns the "local" name of the current node.// For a tag like <foo:bar>, this is the string "bar".std::string NodeName();// Returns the name of the current node.// For a tag like <foo:bar>, this is the string "foo:bar".std::string NodeFullName();// When pointing at a tag, retrieves the value of an attribute.// Returns false on failure.// E.g. for <foo bar:baz="a">, NodeAttribute("bar:baz", &value)// returns true and |value| is set to "a".bool NodeAttribute(const char* name, std::string* value);// Populates |attributes| with all the attributes of the current tag and// returns true. Note that namespace declarations are not reported.// Returns false if there are no attributes in the current tag.bool GetAllNodeAttributes(std::map<std::string, std::string>* attributes);// Populates |namespaces| with all the namespaces (prefix/URI pairs) declared// in the current tag and returns true. Note that the default namespace, if// declared in the tag, is populated with an empty prefix.// Returns false if there are no namespaces declared in the current tag.bool GetAllDeclaredNamespaces(std::map<std::string, std::string>* namespaces);// Sets |content| to the content of the current node if it is a// text, cdata, or significant-whitespace node, respectively.// Returns true if the current node is a node of the corresponding, false// otherwise.bool GetTextIfTextElement(std::string* content);bool GetTextIfCDataElement(std::string* content);bool GetTextIfSignificantWhitespaceElement(std::string* content);// Returns true if the node is an element (e.g. <foo>). Note this returns// false for self-closing elements (e.g. <foo/>). Use IsEmptyElement() to// check for those.bool IsElement();// Returns true if the node is a closing element (e.g. </foo>).bool IsClosingElement();// Returns true if the current node is an empty (self-closing) element (e.g.// <foo/>).bool IsEmptyElement();// Helper functions not provided by libxml ----------------------------------// Return the string content within an element.// "<foo>bar</foo>" is a sequence of three nodes:// (1) open tag, (2) text, (3) close tag.// With the reader currently at (1), this returns the text of (2),// and advances past (3).// Returns false on error.bool ReadElementContent(std::string* content);// Skip to the next opening tag, returning false if we reach a closing// tag or EOF first.// If currently on an opening tag, doesn't advance at all.bool SkipToElement();private:// Returns the libxml node type of the current node.int NodeType();// A helper function for GetTextIf*Element() functions above.// Checks if the node is the specified `node_type`, and, if so, populates// `content` and returns true.bool GetTextFromNodeIfType(int node_type, std::string* content);// The underlying libxml xmlTextReader._xmlTextReader* reader_;
};

2、保存xml

third_party/libxml/chromium/xml_writer.h


#include <string>extern "C" {
struct _xmlBuffer;
struct _xmlTextWriter;
}// XmlWriter is a wrapper class around libxml's xmlWriter,
// providing a simplified C++ API.
// StartWriting must be called before other methods, and StopWriting
// must be called before GetWrittenString() will return results.
class XmlWriter {public:XmlWriter();~XmlWriter();// Allocates the xmlTextWriter and an xmlBuffer and starts an XML document.// This must be called before any other functions. By default, indenting is// set to true.void StartWriting();// Ends the XML document and frees the xmlTextWriter.// This must be called before GetWrittenString() is called.void StopWriting();// Wrappers around libxml functions -----------------------------------------// All following elements will be indented to match their depth.void StartIndenting();// All follow elements will not be indented.void StopIndenting();// Start an element with the given name. All future elements added will be// children of this element, until it is ended. Returns false on error.bool StartElement(const std::string& element_name);// Ends the current open element. Returns false on error.bool EndElement();// Appends to the content of the current open element.bool AppendElementContent(const std::string& content);// Adds an attribute to the current open element. Returns false on error.bool AddAttribute(const std::string& attribute_name,const std::string& attribute_value);// Adds a new element with name |element_name| and content |content|// to the buffer. Example: <|element_name|>|content|</|element_name|>// Returns false on errors.bool WriteElement(const std::string& element_name,const std::string& content);// Helper functions not provided by xmlTextWriter ---------------------------// Returns the string that has been written to the buffer.std::string GetWrittenString();private:// The underlying libxml xmlTextWriter._xmlTextWriter* writer_;// Stores the output._xmlBuffer* buffer_;
};

例子:

XmlReader xml_reader;//从字符串读取
if (!xml_reader.Load(xml_contents))return ;//从文件读取XmlReader enums_xml_reader;if (!enums_xml_reader.LoadFile(enums_xml.MaybeAsASCII())) {return;}


http://www.ppmy.cn/embedded/153924.html

相关文章

Ubuntu 24.04蓝牙失效之复活

Thinkpad E450 Ubuntu 24.04 蓝牙突然罢工&#xff0c;设置页面蓝牙无法激活(turn on失效&#xff09;。 网上搜索诸多帖子试了一遍都没有解决&#xff0c;直到拜读到这篇[Solved] Bluetooth down and hciconfig hci0 up timeout / Kernel & Hardware / Arch Linux Forums…

C++实现设计模式---代理模式 (Proxy)

代理模式 (Proxy) 代理模式 是一种结构型设计模式&#xff0c;它为其他对象提供一个代理以控制对该对象的访问。代理模式常用于延迟加载、访问控制、智能引用等场景。 意图 提供对某对象的控制。控制对目标对象的访问&#xff0c;通常用于在不改变目标对象的情况下&#xff0…

【PyQt】如何在mainwindow中添加菜单栏

[toc]如何在mainwindow中添加菜单栏 如何在mainwindow中添加菜单栏 主要有两种方法&#xff1a; 1.直接创建mainwindow进行添加 2.使用ui文件加载添加 第二种方法更为常见&#xff0c;可以应用到实际 1.直接创建mainwindow进行添加 import sysfrom PyQt5.QtWidgets import …

Spring Boot + MyBatis Plus 存储 JSON 或 List 列表全攻略

在现代的后端开发中&#xff0c;我们常常需要处理复杂的数据结构&#xff0c;JSON 数据以及列表&#xff08;List&#xff09;数据屡见不鲜。如何高效地使用 Spring Boot 和 MyBatis Plus 来存储这些复杂数据类型&#xff0c;是这篇博客要探讨的重点。 一、为什么要存储 JSON …

CentOS 和 Ubantu你该用哪个

文章目录 **一、CentOS 和 Ubuntu 的详细介绍****1. CentOS****1.1 基本信息****1.2 特点****1.3 缺点** **2. Ubuntu****2.1 基本信息****2.2 特点****2.3 缺点** **二、CentOS 和 Ubuntu 的异同****1. 相同点****2. 不同点****3. 使用体验对比** **三、总结和选择建议** Cent…

科技赋能:多功能气膜综合馆引领场馆新革命—轻空间

在现代体育场馆建设中&#xff0c;如何为运动员提供更佳的比赛环境&#xff0c;为观众营造更舒适的观赛体验&#xff0c;已成为场馆设计的关键课题。而多功能气膜综合馆以其独特的声学优化技术和卓越的场馆功能&#xff0c;成功突破了传统气膜场馆的局限&#xff0c;为运动体验…

macOS 版本对应 Xcode 版本,以及 Xcode 历史版本下载

注&#xff1a;当前页面的所有Xcode下载链接均为苹果官方下载链接 &#xff0c;点击将直接转至苹果官网下载。❤️❤️❤️ Xcode官网&#xff1a;Xcode Releases | xcodereleases.com Xcode版本Xcode发布时间对应macOS版本macOS SDKsiOS SDKswatchOS SDKstvOS SDKs下载Xcode发…

后台管理系统-axios网络请求的封装

此博客是针对开源项目:vue3-element-admin 的学习记录,为了帮助自己理清开发这个系统的逻辑. 安装依赖 npm install axios , qsAxios实例封装 // 创建 axios 实例 ,同时给出一些预设配置&#xff0c;比如baseURL&#xff0c;超时时间等等 const service axios.create({base…