1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

boostでxmlを読み込んで扱う

Last updated at Posted at 2015-09-29

自分用メモ。

a.cpp

# include <stdio.h>

# include <boost/property_tree/ptree.hpp>
# include <boost/property_tree/xml_parser.hpp>

# include <sstream>
# include <string>

const static std::string XML = "\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
<soapenv:Envelope xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\
	<soapenv:Body>\
		<RetrievePropertiesResponse xmlns=\"urn:vim25\">\
			<returnval>\
				<obj type=\"Datacenter\">datacenter-2</obj>\
				<propSet>\
					<name>hostFolder</name>\
					<val type=\"Folder\" xsi:type=\"ManagedObjectReference\">group-h4</val>\
				</propSet>\
				<propSet>\
					<name>name</name>\
					<val xsi:type=\"xsd:string\">vDatacenter</val>\
				</propSet>\
			</returnval>\
			<returnval>\
				<obj type=\"Datacenter\">datacenter-968</obj>\
				<propSet>\
					<name>hostFolder</name>\
					<val type=\"Folder\" xsi:type=\"ManagedObjectReference\">group-h970</val>\
				</propSet>\
				<propSet>\
					<name>name</name>\
					<val xsi:type=\"xsd:string\">新規データセンター</val>\
				</propSet>\
			</returnval>\
		</RetrievePropertiesResponse>\
	</soapenv:Body>\
</soapenv:Envelope>\
";

int main( int , char ** ) {
	
	boost::property_tree::ptree pt;
	std::stringstream ss( XML );
	boost::property_tree::read_xml( ss, pt );
	
	for( auto &it : pt.get_child( "soapenv:Envelope.soapenv:Body.RetrievePropertiesResponse" ) ) {
		
		if( it.first != "returnval" ) {
			
			continue;
			
		}
		
		std::string type = it.second.get<std::string>( "obj.<xmlattr>.type" );
		std::string entity = it.second.get<std::string>( "obj" );
		
		printf( "%s\t%s\n", type.c_str(), entity.c_str() );
		
		for( auto &itt: it.second ) {
			
			if( itt.first != "propSet" ) {
				
				continue;
				
			}
			
			std::string name = itt.second.get<std::string>( "name" );
			std::string val = itt.second.get<std::string>( "val" );
			
			printf( "\t%s\t%s\n", name.c_str(), val.c_str() );
			
		}
		
	}
	
	return( 0 );
	
}

/*
[todanano@localhost tmp]$ ./a.out
Datacenter      datacenter-2
        hostFolder      group-h4
        name    vDatacenter
Datacenter      datacenter-968
        hostFolder      group-h970
        name    新規データセンター
[todanano@localhost tmp]$
*/


1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?