tinyxml读取xml文件

xml中每个元素element的组成

  • 唯一的name
  • 多个attribute(每个attribute包含name和value)
  • 注释comment
  • 文本Text
  • 多个子element
<?xml version="1.0" encoding="UTF-8"?>
<ffprobe>
    <format filename=".\Westworld.S03E01.720p.WEB.H264-XLF[eztv].mkv" nb_streams="3" nb_programs="0" format_name="matroska,webm" format_long_name="Matroska / WebM" start_time="0.000000" duration="4690.801000" size="1320094225" bit_rate="2251375" probe_score="100">
        <tag key="encoder" value="libebml v1.3.9 + libmatroska v1.5.2"/>
        <tag key="creation_time" value="2020-03-16T01:00:32.000000Z"/>
    </format>
</ffprobe>
#include <iostream>
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;

int main() {
	const char* filename = "F:/_workspace/c++/readxml/format.xml";
	XMLDocument doc;
	XMLError ret = doc.LoadFile(filename);
	if (ret != XML_SUCCESS) {
		printf("load xml return %d\n", ret);
		return 0;
	}
	XMLElement* root = doc.RootElement();
	printf("rootnode: %s\n", root->Name());
	XMLElement* format = root->FirstChildElement("format");
	printf("childnode: format, attr filename=%s\n", format->Attribute("filename"));
	printf("attr nb_streams=%u\n", format->UnsignedAttribute("nb_streams"));
	//遍历所有子元素
	for (XMLElement* tag = format->FirstChildElement(); tag != nullptr; tag = tag->NextSiblingElement()) {
		printf("k=%s, v=%s\n", tag->Attribute("key"), tag->Attribute("value"));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yuejisuo1948/article/details/112549944
今日推荐