源代码如下:
#include<windows.h> #include "tinyxml2.h" #include <stdio.h> #include <string> #include <vector> //using namespace std; using namespace tinyxml2; int Check_Root_Attr(XMLElement* root) { const char* expect_root_name = "TestResults"; const char* expect_xmlns = "http://www.ieee.org/ATML/2007/TestResults"; const char* expect_xmlns_c = "http://www.ieee.org/ATML/2007/02/Common"; const char* expect_xmlns_xsi= "http://www.w3.org/2001/XMLSchema-instance"; const XMLAttribute *attr1 = root->FirstAttribute(); if(!attr1) return -1; printf("%s:%s\n", attr1->Name(),attr1->Value()); const XMLAttribute *attr2 = attr1->Next(); if(!attr2) return -2; printf("%s:%s\n", attr2->Name(), attr2->Value()); const XMLAttribute *attr3 = attr2->Next(); if(!attr3) return -3; printf("%s:%s\n", attr3->Name(), attr3->Value()); return 0; } const char* Get_Operator_ID(XMLElement* root) { if(!root) return 0; const char* expect_node_name = "Personnel"; const char* expect_attr_name = "SystemOperator"; XMLElement* child_element =root->FirstChildElement(expect_node_name); if(!child_element) return 0; XMLElement *element = child_element->FirstChildElement(expect_attr_name); if(!element) return 0; return element->Attribute("ID"); } struct TestProgram { std::string Productline; std::string Product; std::string version; std::string name; }; const char* Get_Test_Station(XMLElement* root) { const char* expect_node_name = "TestStation"; const char* expect_attr_name = "AteName"; XMLNode* node = root->FirstChild(); while(node) { const char* node_name = node->Value(); if(strcmp(node_name, expect_node_name) == 0) { XMLElement *element = node->ToElement(); const char* ate_name = element->Attribute(expect_attr_name); return ate_name; } node = node->NextSibling(); } return 0; } TestProgram Get_Test_Program(XMLElement* root) { TestProgram program_info; const char* expect_node_name = "TestProgram"; const char* expect_productline_name = "Productline"; const char* expect_product_name = "Product"; const char* expect_version_name = "version"; const char* expect_program_name = "name"; XMLNode* node = root->FirstChild(); while(node) { const char* node_name = node->Value(); if(strcmp(node_name, expect_node_name) == 0) { XMLElement *element = node->ToElement(); const char* productline_name = element->Attribute(expect_productline_name); const char* product_name = element->Attribute(expect_product_name); if(product_name) program_info.Product.assign(product_name); if(productline_name) program_info.Productline.assign(productline_name); XMLElement *child_element = element->FirstChildElement(); if(!child_element) return program_info; const char* version = child_element->Attribute(expect_version_name); const char* program_name = child_element->Attribute(expect_program_name); if(program_name) program_info.name.assign(program_name); if(version) program_info.version.assign(version); return program_info; break; } node = node->NextSibling(); } return program_info; } struct UUT { std::string UUTType; std::string SerialNumber; std::string ItemName; std::string ItemCode; }; UUT Get_UUT_Info(XMLElement* root) { UUT uut_info; const char* expect_node_name = "UUT"; const char* expect_type_name = "UutType"; const char* expect_serialnumber = "SerialNumber"; const char* expect_item_name = "ItemName"; const char* expect_item_code = "ItemCode"; XMLNode* node = root->FirstChild(); while(node) { const char* node_name = node->Value(); if(strcmp(node_name, expect_node_name) == 0) { XMLElement *element = node->ToElement(); const char* uut_type = element->Attribute(expect_type_name); uut_info.UUTType.assign(uut_type); XMLElement *child_element = element->FirstChildElement(); if(!child_element) return uut_info; const char* serial_number = child_element->GetText(); if(serial_number) uut_info.SerialNumber.assign(serial_number); child_element = child_element->NextSiblingElement(); if(!child_element) return uut_info; const char* item_name = child_element->GetText(); if(item_name) uut_info.ItemName.assign(item_name); child_element = child_element->NextSiblingElement(); if(!child_element) return uut_info; const char* item_code = child_element->GetText(); if(item_code) uut_info.ItemCode.assign(item_code); return uut_info; break; } node = node->NextSibling(); } return uut_info; } struct WorkOrder { std::string name; std::string type; std::string value; }; WorkOrder Get_WorkOrder(XMLElement* root) { WorkOrder work_order; const char* expect_node_name = "WorkOrder"; const char* expect_subnode_name = "WorkItem"; const char* expect_sub_sub_name = "c:Dtaum"; const char* expect_sub_sub_sub_name = "c:Value"; const char* expect_value_type = "xsi:type"; XMLNode* node = root->FirstChild(); while(node) { const char* node_name = node->Value(); if(strcmp(node_name, expect_node_name) == 0) { XMLElement *element = node->ToElement(); XMLElement *child_element = element->FirstChildElement(); if(!child_element) return work_order; while(child_element) { const char* child_name = child_element->Value(); if(strcmp(child_name, expect_subnode_name) == 0) { const char* child_attr = child_element->Attribute("name"); if(!child_attr) return work_order; work_order.name.assign(child_attr); XMLElement *child_child_element = child_element->FirstChildElement(); if(!child_child_element) return work_order; while(child_child_element) { const char* child_child_name = child_child_element->Value(); if(strcmp(child_child_name, expect_sub_sub_name)) { const char* child_child_attr = child_child_element->Attribute(expect_value_type); if(!child_child_attr) return work_order; work_order.type.assign(child_child_attr); XMLElement *child_child_child_elemet = child_child_element->FirstChildElement(); if(!child_child_child_elemet) return work_order; const char* child_child_child_name = child_child_child_elemet->Value(); if(strcmp(child_child_child_name, expect_sub_sub_sub_name)) return work_order; const char* child_child_child_attr = child_child_child_elemet->GetText(); if(child_child_child_attr) work_order.value.assign(child_child_child_attr); return work_order; } child_child_element = child_child_element->NextSiblingElement(); } } child_element = child_element->NextSiblingElement(); } } node = node->NextSibling(); } return work_order; } struct ResultSet { std::string ID; std::string startDateTime; std::string endDateTime; std::string operationSequence; std::string siteCode; std::string Description; }; ResultSet Get_Result_Set(XMLElement* root, XMLNode** result_set_node) { ResultSet result_set; const char* expect_node_name = "ResultSet"; const char* expect_attr_id = "ID"; const char* expect_attr_start = "startDateTime"; const char* expect_attr_end = "endDateTime"; const char* expect_attr_sequence = "operationSequence"; const char* expect_attr_site= "siteCode"; XMLNode* node = root->FirstChild(); while(node) { const char* node_name = node->Value(); if(strcmp(node_name, expect_node_name) == 0) { if(*result_set_node) *result_set_node = node; XMLElement *element = node->ToElement(); const char* attr_id = element->Attribute("ID"); const char* attr_start = element->Attribute("startDateTime"); const char* attr_end = element->Attribute("endDateTime"); const char* attr_sequence = element->Attribute("operationSequence"); const char* attr_site= element->Attribute("siteCode"); XMLElement *child_element = element->FirstChildElement(); if(child_element) { const char* text = child_element->GetText(); if(text) result_set.Description.assign(text); } result_set.ID.assign(attr_id); if(attr_id) result_set.startDateTime.assign(attr_start); if(attr_end) result_set.endDateTime.assign(attr_end); if(attr_sequence) result_set.operationSequence.assign(attr_sequence); if(attr_site) result_set.siteCode.assign(attr_site); return result_set; } node = node->NextSibling(); } return result_set; } struct TopResult { std::string BoardFlag; std::string ID; std::string Value; }; TopResult Get_Top_Result(XMLNode* result_set) { TopResult top_result; if(!result_set) return top_result; XMLElement* element = result_set->ToElement(); XMLElement *child_element = element->FirstChildElement(); if(!child_element) return top_result; XMLElement *next_element = child_element->NextSiblingElement(); if(!next_element) return top_result; const char* board_flag = next_element->Attribute("BoardFlag"); const char* ID = next_element->Attribute("ID"); if(board_flag) top_result.BoardFlag.assign(board_flag); if(ID) top_result.ID.assign(ID); XMLElement* next_element_child = next_element->FirstChildElement(); if(!next_element_child) return top_result; const char* value = next_element_child->GetText(); if(value) top_result.Value.assign(value); return top_result; } struct ItemTest { std::string ID; std::string name; std::string startDateTime; std::string endDateTime; XMLElement *item_test_result; XMLElement *self; }; std::vector<ItemTest> Get_Test_Items(XMLNode* result_set) { const char* expect_node_name = "ItemTest"; std::vector<ItemTest> items; if(!result_set) return items; XMLElement *result_set_element = result_set->ToElement(); XMLElement *child_element = result_set_element->FirstChildElement(); while(child_element) { const char* name = child_element->Value(); if(!name) return items; if(strcmp(expect_node_name, name) == 0) { const char* id = child_element->Attribute("ID"); const char* name = child_element->Attribute("name"); const char* start = child_element->Attribute("startDateTime"); const char* end = child_element->Attribute("endDateTime"); XMLElement* p = child_element->FirstChildElement(); ItemTest item; if(id) item.ID.assign(id); if(name) item.name.assign(name); if(start) item.startDateTime.assign(start); if(end) item.endDateTime.assign(end); if(p) item.item_test_result = p; item.self = child_element; items.push_back(item); } child_element = child_element->NextSiblingElement(); } return items; } struct SubItemTest { std::string ID; std::string name; XMLElement* sub_result; XMLElement* self; }; std::vector<SubItemTest> Get_Sub_Items(XMLElement* item_test) { const char* expect_node_name = "SubItemTest"; std::vector<SubItemTest> sub_items; if(!item_test) return sub_items; XMLElement *child_element = item_test->FirstChildElement(); while(child_element) { const char* name = child_element->Value(); if(!name) return sub_items; if(strcmp(expect_node_name, name) == 0) { const char* id = child_element->Attribute("ID"); const char* name = child_element->Attribute("name"); XMLElement* p = child_element->FirstChildElement(); SubItemTest item; if(id) item.ID.assign(id); if(name) item.name.assign(name); if(p) item.sub_result = p; item.self = child_element; sub_items.push_back(item); } child_element = child_element->NextSiblingElement(); } return sub_items; } struct SubItemResult { std::string Outcome; std::string value; std::string low_limit; std::string high_limit; }; SubItemResult Get_Sub_Item_Result(XMLElement* sub_item_result) { SubItemResult sub_result; if(!sub_item_result) return sub_result; const char* out_come = "Outcome"; const char* test_data = "TestData"; XMLElement* child_element = sub_item_result->FirstChildElement(); while(child_element) { const char* name = child_element->Value(); if(strcmp(out_come, name) == 0) { const char* pf = child_element->Attribute("value"); if(pf) sub_result.Outcome.assign(pf); } if(strcmp(name, test_data) == 0) { XMLElement* child_child_element = child_element->FirstChildElement(); if(child_child_element) { const char* value = child_child_element->Attribute("value"); if(value) sub_result.value.assign(value); } } child_element = child_element->NextSiblingElement(); } return sub_result; } enum TEXT_COLOR : WORD { red = FOREGROUND_RED|FOREGROUND_INTENSITY, green = FOREGROUND_GREEN|FOREGROUND_INTENSITY, blue = FOREGROUND_BLUE|FOREGROUND_INTENSITY, }; void set_test_color(HANDLE handle, TEXT_COLOR color) { SetConsoleTextAttribute(handle, color); } int main(int argc, char* argv[]) { HANDLE hStdout; CONSOLE_SCREEN_BUFFER_INFO csbi; hStdout = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hStdout, &csbi); set_test_color(hStdout, blue); auto set_color_chr = [=](const char* chr) { if(!chr) set_test_color(hStdout, red); else set_test_color(hStdout, green); }; auto set_color_str = [=](const std::string& str) { if(str.empty()) set_test_color(hStdout, red); else set_test_color(hStdout, green); }; tinyxml2::XMLDocument doc; XMLError err= doc.LoadFile("D:\\Exercise\\Cplusplus\\Test_Read_XML\\read_xml\\test.xml"); if(err != 0) { set_test_color(hStdout, red); printf("Error: %s\n", doc.ErrorName()); set_test_color(hStdout, blue); return 0; } XMLNode *first_node =doc.FirstChild(); printf("version=%s\n", first_node->Value()); XMLElement *root_node = doc.RootElement(); Check_Root_Attr(root_node); const char* operator_id = Get_Operator_ID(root_node); set_color_chr(operator_id); printf("Operator ID=%s\n", operator_id); const char* station_name = Get_Test_Station(root_node); set_color_chr(station_name); printf("Station Name=%s\n", station_name); TestProgram program = Get_Test_Program(root_node); set_color_str(program.Productline); printf("Productline=%s\n",program.Productline.c_str()); set_color_str(program.Product); printf("Product=%s\n", program.Product.c_str()); set_color_str(program.name); printf("TestProgram=%s\n", program.name.c_str()); set_color_str(program.version); printf("Version=%s\n", program.version.c_str()); UUT uut = Get_UUT_Info(root_node); set_color_str(uut.UUTType); printf("UutType=%s\n", uut.UUTType.c_str()); set_color_str(uut.SerialNumber); printf("SerialNumber=%s\n", uut.SerialNumber.c_str()); set_color_str(uut.ItemName); printf("ItemName=%s\n", uut.ItemName.c_str()); set_color_str(uut.ItemCode); printf("ItemCode=%s\n", uut.ItemCode.c_str()); set_color_str(program.version); WorkOrder workorder = Get_WorkOrder(root_node); set_color_str(workorder.name); printf("WorkItem Name=%s\n", workorder.name.c_str()); set_color_str(workorder.type); printf("Data Type=%s\n", workorder.type.c_str()); set_color_str(workorder.value); printf("Data Value=%s\n", workorder.value.c_str()); XMLNode *result_set_node; ResultSet result_set = Get_Result_Set(root_node, &result_set_node); set_test_color(hStdout, blue); printf("ResultSetNode Name=%s\n", result_set_node->Value()); set_color_str(result_set.ID); printf("ResultSet ID=%s\n", result_set.ID.c_str()); set_color_str(result_set.startDateTime); printf("ResultSet startDataTime=%s\n", result_set.startDateTime.c_str()); set_color_str(result_set.endDateTime); printf("ResultSet endDataTime=%s\n", result_set.endDateTime.c_str()); set_color_str(result_set.operationSequence); printf("ResultSet operationSequence=%s\n", result_set.operationSequence.c_str()); set_color_str(result_set.siteCode); printf("ResultSet SiteCode=%s\n", result_set.siteCode.c_str()); set_color_str(result_set.Description); printf("ResultSet Description=%s\n", result_set.Description.c_str()); TopResult top_result = Get_Top_Result(result_set_node); set_color_str(top_result.BoardFlag); printf("TestResult Board Flag=%s\n", top_result.BoardFlag.c_str()); set_color_str(top_result.ID); printf("TestResult ID=%s\n", top_result.ID.c_str()); set_color_str(top_result.Value); printf("TestResult Value=%s\n", top_result.Value.c_str()); std::vector<ItemTest> items = Get_Test_Items(result_set_node); for(unsigned int i = 0; i < items.size(); i++) { ItemTest item = items[i]; set_color_str(item.ID); printf("Test Item ID=%s\n", item.ID.c_str()); set_color_str(item.name); printf("Test Item name=%s\n", item.name.c_str()); set_color_str(item.startDateTime); printf("Test Item startDataTime=%s\n", item.startDateTime.c_str()); set_color_str(item.endDateTime); printf("Test Item endDataTime=%s\n", item.endDateTime.c_str()); XMLElement* result = item.item_test_result; if(result) { const char* Board_Flag = result->Attribute("BoardFlag"); const char* SerialNumber = result->Attribute("SerialNumber"); const char* ID = result->Attribute("ID"); const char* ValueFlag = result->Attribute("ValueFlag"); char pf[16]; XMLElement* result_child = result->FirstChildElement(); if(result_child) sprintf(pf, "%s", result_child->Attribute("value")); else sprintf(pf, "unknown"); set_test_color(hStdout, blue); printf("i=%d, BoardFlg=%s, SerialNumber=%s, ID=%s, ValueFlag=%s, Value=%s\n",i, Board_Flag,SerialNumber,ID, ValueFlag, pf ); } std::vector<SubItemTest> subitems = Get_Sub_Items(item.self); for(unsigned int n = 0; n < subitems.size(); n++) { SubItemTest sub_item = subitems[n]; SubItemResult sub_result = Get_Sub_Item_Result(sub_item.sub_result); set_test_color(hStdout, blue); printf("ID=%s, Name=%s, Result=%s, Value=%s\n",sub_item.ID.c_str(), sub_item.name.c_str(), sub_result.Outcome.c_str(), sub_result.value.c_str()); } } return 0; }
使用C++写代码,配合STL,不那么关注效率的时候,可以快速利用现有的C++或者C代码库实现一些小功能。
标签:char,const,name,tinyxml2,测试数据,element,ATML,result,child From: https://www.cnblogs.com/integrated-circuit-testing/p/17075116.html