| #include <iostream> |
| #include"tinyxml2.h" |
| #include<string> |
|
|
| using namespace std; |
| using namespace tinyxml2; |
| string a; |
| string b; |
| string c; |
| string d; |
| string a1; |
|
|
|
|
| void createXml() |
| { |
| |
| |
| |
| tinyxml2::XMLDocument xml; |
| tinyxml2::XMLDeclaration* declaration = xml.NewDeclaration(); |
| xml.InsertFirstChild(declaration); |
|
|
| |
| |
| |
| tinyxml2::XMLElement* rootNode = xml.NewElement("computer"); |
| xml.InsertEndChild(rootNode); |
|
|
| |
| |
| |
| tinyxml2::XMLElement* root_1_NodeComputerMonitor = xml.NewElement("ComputerMonitor"); |
| tinyxml2::XMLElement* root_1_NodeKeyboard = xml.NewElement("Keyboard"); |
| tinyxml2::XMLElement* root_1_CPU = xml.NewElement("CPU"); |
|
|
| |
| |
| |
| tinyxml2::XMLText* text_root_1_NodeComputerMonitor = xml.NewText("SAMSUNG"); |
| root_1_NodeComputerMonitor->InsertFirstChild(text_root_1_NodeComputerMonitor); |
|
|
| tinyxml2::XMLText* text_root_1_root_1_CPU = xml.NewText("intel"); |
| root_1_CPU->InsertFirstChild(text_root_1_root_1_CPU); |
|
|
| |
| |
| |
| root_1_NodeComputerMonitor->SetAttribute("size", "15"); |
| root_1_CPU->SetAttribute("series", "XEON"); |
|
|
| |
| |
| |
| tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = xml.NewElement("KeyboardAttribute"); |
| |
| |
| |
| |
| tinyxml2::XMLText* text_root_2_NodeKeyboardAttribute = xml.NewText("cherry Mechanical Keyboard"); |
| root_2_NodeKeyboardAttribute->InsertFirstChild(text_root_2_NodeKeyboardAttribute); |
|
|
| |
| |
| |
| rootNode->InsertEndChild(root_1_NodeComputerMonitor); |
| rootNode->InsertEndChild(root_1_NodeKeyboard); |
| rootNode->InsertEndChild(root_1_CPU); |
| |
| |
| |
| root_1_NodeKeyboard->InsertEndChild(root_2_NodeKeyboardAttribute); |
|
|
| |
| |
| |
| xml.SaveFile("computer.xml"); |
| } |
| |
| void deCodeXml() |
| { |
| |
| |
| |
| tinyxml2::XMLDocument xml; |
|
|
| |
| |
| |
| if (xml.LoadFile("computer.xml") != XML_SUCCESS) |
| { |
| return; |
| } |
|
|
| |
| |
| |
| tinyxml2::XMLElement* rootNode = xml.RootElement(); |
| if (rootNode == NULL) { |
| return; |
| } |
| |
| |
| |
| |
| tinyxml2::XMLElement* root_1_NodeComputerMonitor = rootNode->FirstChildElement("ComputerMonitor"); |
| std::string text_root_1_NodeComputerMonitor = root_1_NodeComputerMonitor->GetText(); |
| std::string text_root_1_size = root_1_NodeComputerMonitor->Attribute("size"); |
| a=text_root_1_NodeComputerMonitor.c_str(); |
| a1=text_root_1_size; |
| |
|
|
| |
| |
| |
| tinyxml2::XMLElement* root_1_NodeKeyboard = rootNode->FirstChildElement("Keyboard"); |
| tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = root_1_NodeKeyboard->FirstChildElement("KeyboardAttribute"); |
| std::string text_root_2_NodeKeyboardAttribute = root_2_NodeKeyboardAttribute->GetText(); |
| b = text_root_2_NodeKeyboardAttribute.c_str(); |
|
|
| tinyxml2::XMLElement* root_1_NodeCPU = rootNode->FirstChildElement("CPU"); |
| std::string text_root_1_NodeCPU = root_1_NodeCPU->GetText(); |
| std::string text_root_1_series = root_1_NodeCPU->Attribute("series"); |
| c=text_root_1_series; |
| d=text_root_1_NodeCPU; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| struct ComputerMonitor |
| { |
| string size; |
| string type; |
| }; |
| struct CPU |
| { |
| string series; |
| string type; |
|
|
| }; |
| struct computer |
| { |
| struct ComputerMonitor cm1 ; |
| string Keyboard; |
| struct CPU cpu1; |
|
|
| }; |
| int main() |
| { |
| |
| |
| |
|
|
|
|
|
|
| |
| deCodeXml(); |
| |
| |
| |
| struct computer computer1; |
| computer1.cm1.size=a ; |
| computer1.cm1.type=a1; |
| computer1.Keyboard = b; |
| computer1.cpu1.series=c; |
| computer1.cpu1.type=d; |
| cout<<computer1.cm1.size<<endl; |
| cout<<computer1.cm1.type<<endl; |
| cout<<computer1.Keyboard<<endl; |
| cout<<computer1.cpu1.series<<endl; |
| cout<<computer1.cpu1.type<<endl; |
| return 0; |
| |
| } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|