A simple example of C + + calling tinyxl2 to traverse all xml nodes and use map output

A simple example of C + + calling tinyxl2 to traverse all xml nodes and use map output

This example only needs 3 files in total

Hello, this paper aims to implement a simple example of C + + calling tinyxl2 to traverse all xml nodes and use map output;

The first file is the main.cpp file written by myself;
The second file is ElectronicProduct.xml

The third and fourth files are tinyxl2. H and tinyxl2. CPP, which are the packages to be called

To get all the codes, please go to github download address:

Get all codes
Method 1: download the link: link You can also copy the link browser to open it (https://github.com/sdjmcgsjzy/tinyxl2example.git)

Method 2: main.cc copy the content of this blog
ElectronicProduct.xml copy the content of this blog
Tinyxl2. H and tinyxl2. CPP can be found in the link That is, copy and download in the blog (https://blog.csdn.net/qq_/article/details/104803508)

Bily Bily has a detailed video explanation address:

The link will be sent out later

main.cpp

The main.cpp file you need to write
#include <iostream>
#include"tinyxml2.h"
#include <map>
using namespace std;
using namespace tinyxml2;

//Core code, recursively implement the node reading xml
void GetElementValue(XMLElement *element , std::map<std::string, std::string> &itemID_value_map)
{
	for (XMLElement* currenteleElement = element->FirstChildElement(); currenteleElement; currenteleElement = currenteleElement->NextSiblingElement())
	{
		XMLElement* tmpele = currenteleElement;
		if (currenteleElement->GetText() != NULL)
		{
			cout << "ItemID = " << currenteleElement->Attribute("ItemID") << "---Value = " << currenteleElement->GetText() << endl;
			itemID_value_map[currenteleElement->Attribute("ItemID")] = currenteleElement->GetText();
		}
		//Determine whether the node is empty. If not, continue recursion
		if (!tmpele->NoChildren())
			GetElementValue(tmpele,itemID_value_map);
	}
}

//Read the nodes in the xml file recursively and save them in the map
std::map<std::string,std::string> CycleReadAllNode(std::string xmlPath) 
{
	static std::map<std::string, std::string> itemID_value_map;
	itemID_value_map.clear();

	tinyxml2::XMLDocument xml;
	if (xml.LoadFile(xmlPath.c_str()) != XML_SUCCESS)
	{
		return itemID_value_map; //return empty map
	}
	tinyxml2::XMLElement* rootNode = xml.RootElement();
	if (rootNode == NULL) {
		return itemID_value_map; //return empty map
	}
	GetElementValue(rootNode, itemID_value_map);
	return itemID_value_map;
}


int main()
{
	std::map<std::string, std::string>  maps = CycleReadAllNode("ElectronicProduct.xml");
	//Traversing map
	cout << "-----------------------begin Loop traversal node------------------------" << endl;
	std::map<std::string, std::string>::iterator iter;
	for (iter = maps.begin(); iter != maps.end(); iter++)
	{
		std::cout << "itemId = " << (iter->first).c_str() << "-------value = " << (iter->second).c_str() << std::endl;
	}
	cout << "-----------------------end   Loop traversal node------------------------" << endl;
    return 0;
}

ElectronicProduct.xml

The ElectronicProduct.xml file you need to write
<?xml version="1.0" encoding="UTF-8"?>
<ElectronicProduct>
    <computer ItemID = "DELL">8000yuan</computer>
    <computer ItemID = "MAC PRO">16000yuan</computer>
    <pad ItemID = "ipadmin3">1600yuan</pad>
 
</ElectronicProduct>

Other

Thank you for your reading. If there is anything wrong with this article, please leave a message;

Published 2 original articles · praised 2 · visited 24
Private letter follow

Tags: xml github Attribute git

Posted on Thu, 12 Mar 2020 20:54:21 -0700 by jabapyth