tinyxml2解析XML文件

前端之家收集整理的这篇文章主要介绍了tinyxml2解析XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
	tinyxml2::XMLDocument xmlDoc;
	tinyxml2::XMLError error = xmlDoc.LoadFile(cfgFile.c_str());
	if (error != tinyxml2::XML_NO_ERROR) {
		throw ConfigurationFileNotFound("Configuration File is Failed to open");
	}

	//解析文档
	// HOST
	tinyxml2::XMLElement* root = xmlDoc.FirstChildElement("config")->FirstChildElement( "web");
	const char * contextStr = root->FirstChildElement("host")->GetText();
	hostConfig.host = contextStr;

	contextStr = root->FirstChildElement( "port" )->GetText();
	hostConfig.port = atoi( contextStr );

	// DB
	root = xmlDoc.FirstChildElement("config")->FirstChildElement("db");
	contextStr = root->FirstChildElement("host")->GetText();
	dbConfig.host = contextStr;

	contextStr = root->FirstChildElement("port")->GetText();
	dbConfig.port = atoi( contextStr );

	contextStr = root->FirstChildElement("username")->GetText();
	dbConfig.username = contextStr;

	contextStr = root->FirstChildElement("password")->GetText();
	dbConfig.password = contextStr;

	// SFTP
	root = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "sftp" );
	contextStr = root->FirstChildElement( "host" )->GetText();
	sftpConfig.host = contextStr;

	contextStr = root->FirstChildElement("username")->GetText();
	sftpConfig.username = contextStr;

	contextStr = root->FirstChildElement("password")->GetText();
	sftpConfig.password = contextStr;

	contextStr = root->FirstChildElement("remote")->GetText();
	sftpConfig.remote= contextStr;
	
	contextStr = root->FirstChildElement("local")->GetText();
	sftpConfig.local = contextStr;

	// Path
	root = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "path" );
	resultPath = root->FirstChildElement( "result" )->GetText();
	reportPath = root->FirstChildElement( "report" )->GetText();

	// Resend Pack
	root = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "multipack" );
	multiPackResendConfig.timeout = atoi(root->FirstChildElement("timeout")->GetText());
	multiPackResendConfig.maxcount = atoi(root->FirstChildElement("maxcount")->GetText());
	root = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "singlepack" );
	singlePackResendConfig.timeout = atoi(root->FirstChildElement("timeout")->GetText());
	singlePackResendConfig.maxcount = atoi(root->FirstChildElement("maxcount")->GetText());

下面是xml文件

<config>
	<web>
		<host>172.16.6.70</host>
		<port>5055</port>
	</web>
	<db>
		<host>172.16.6.78</host>
		<port>5432</port>
		<username>postgres</username>
		<password>postgres</password>
		<dbname>RemoteAssert</dbname>
	</db>
	<sftp>
		<host>172.16.6.78</host>
		<username>root</username>
		<password>*********</password>
		<remote>/opt</remote>
		<local>/home/liujiayu/diagnosefile/cmdanddatafile</local>
	</sftp>
	<path>
		<result>/home/liujiayu/diagnosefile/result</result>
		<report>/home/liujiayu/diagnosefile/report</report>
	</path>
	<multipack>
		<timeout>10</timeout>
		<maxcount>3</maxcount>
	</multipack>
	<singlepack>
		<timeout>3</timeout>
		<maxcount>3</maxcount>
	</singlepack>
</config>
原文链接:https://www.f2er.com/xml/295532.html

猜你在找的XML相关文章