字符集Unicode pugixml 读取某个节点下属性文本
时间: 2023-11-27 13:51:09 浏览: 44
假设你要读取的节点是`<book isbn="1234567890">The Hitchhiker's Guide to the Galaxy</book>`,其中`isbn`是属性,文本为`The Hitchhiker's Guide to the Galaxy`。
你可以使用pugixml库的`attribute()`和`text()`函数来读取属性和文本。示例代码如下:
```c++
#include "pugixml.hpp"
#include <iostream>
int main()
{
const char* xml = "<book isbn=\"1234567890\">The Hitchhiker's Guide to the Galaxy</book>";
pugi::xml_document doc;
if (!doc.load_string(xml))
{
std::cerr << "Failed to parse XML" << std::endl;
return 1;
}
pugi::xml_node book_node = doc.child("book");
std::string isbn = book_node.attribute("isbn").value();
std::string text = book_node.text().get();
std::cout << "ISBN: " << isbn << std::endl;
std::cout << "Text: " << text << std::endl;
return 0;
}
```
输出结果如下:
```
ISBN: 1234567890
Text: The Hitchhiker's Guide to the Galaxy
```
注意:在实际使用中,你需要根据自己的需求进行异常处理和错误检查。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)