tinyxml解析后存放至结构体

struct ShelfInfo
{
	int port;
	int addslot[5];
};

ShelfInfo g_shelfinfo[5] = {0};

int example_3()
{
	static const char* xml =
		""
		""
		"2"
			"1"
				"3"
				"4"
				"5"
			""
			"4"
				"6"
				"7"
				"8"
			""
		"";

	XMLDocument doc;
	doc.Parse( xml );

	XMLElement* playElement = doc.FirstChildElement( "PLAY" );
	const char* play = playElement->GetText();
	printf( "Value of play (1): %s\n", play );

	XMLText* textNode = playElement->FirstChild()->ToText();
	const char* portNode = textNode->Value();
	printf( "Value of play (2): %s\n", portNode );

	XMLElement* tempElement = doc.FirstChildElement( "PLAY")->FirstChildElement( "PORT" );
	XMLElement* tempiElement = doc.FirstChildElement( "PLAY")->FirstChildElement( "PORT" )->FirstChildElement( "SLOT" );

	for (int i = 0; i< atoi(play); i++)
	{	
		if(tempElement != NULL)
		{	
			const char* temp = tempElement->GetText();
			g_shelfinfo[i].port = atoi(temp);
			printf( "tempnode : %s\n", temp );

			tempiElement = tempElement->FirstChildElement( "SLOT" );

			for(int j = 0; j < 3; j++)
			{
				if(tempiElement != NULL)
				{	
					const char* tempi = tempiElement->GetText();
					printf(" %s ", tempi);
					g_shelfinfo[i].addslot[j] = atoi(tempi);
					tempiElement = tempiElement->NextSiblingElement();
				}
			}
			printf( "=======\n");

			tempElement = tempElement->NextSiblingElement();

			//printf("tempElement->getText = %s\n",tempiElement->GetText());
		}

	}


	return doc.ErrorID();
}


struct ShelfInfo g_C2[]=
{
	{1,{3,4,5}},
	{2,{7,8,9}},
};

int main()
{

	example_3();

	printf("mytest g_shelfinfo = %d\n",g_shelfinfo[1].addslot[1]);

	system("pause");
	return 0;
}

 

你可能感兴趣的:(VC++)