读取XML文件信息

#include <iostream>
#include <string>
#include "tinyxml.h"
#include "tinystr.h"
using namespace std;


int main()
{
//------------------------------------------------
//        Read information from xml file.
//------------------------------------------------
const char* filepath = "read_multi_IDs.xml";
TiXmlDocument doc(filepath);
bool loadOkay = doc.LoadFile();
if (!loadOkay) {
printf( "Could not load test file %s. Error='%s'. Exiting.\n", filepath,doc.ErrorDesc() );
exit( 1 );
}


cout << "========================================" << endl;
cout << "              日志文件信息      " << endl;
cout << "----------------------------------------" << endl;


TiXmlNode* root = 0;
TiXmlNode* nodeElement = 0;
TiXmlElement* processElement = 0;
TiXmlElement* processInstanceElement = 0;
TiXmlElement* auditTrailElement = 0;
TiXmlElement* dataElement = 0;
TiXmlElement* attributeElement = 0;
TiXmlElement* itemElement = 0;
TiXmlNode* itemNode = 0;
TiXmlText* itemText = 0;


string processInstanceId,attributeName,activeTime,actor,cost;


//get the "WorkflowLog" element.
//It is a child of the document, and can be selected by name.
root = doc.FirstChild("WorkflowLog");
nodeElement = root->ToElement();


//get the "Process" element.
root = nodeElement->FirstChild();
processElement = root->ToElement();






//---------------------------------------------循环---------------------------------------------------
//get the "ProcessInstance" element.
for (processInstanceElement = processElement->FirstChild()->ToElement();
processInstanceElement;
processInstanceElement = processInstanceElement->NextSiblingElement())
{
//get the "ProcessInstance" Attribute
processInstanceId = processInstanceElement->Attribute("id");//获取caseID.
//cout << "ProcessInstance id:" << processInstanceId.c_str() << endl;//输出"ProcessInstance id"的值.


//------------------------------------------------
//   iteratively get "AuditTrailEntry" elements.
//------------------------------------------------
//get the first "AuditTrailEntry" element.
root = processInstanceElement->FirstChildElement();
auditTrailElement = root->ToElement();
for (;auditTrailElement;auditTrailElement = auditTrailElement->NextSiblingElement())
{
cout << "----------------------------------------" << endl;
cout << "ProcessInstance id:" << processInstanceId.c_str() << endl;//输出"ProcessInstance id"的值.
//——get the "Data" element——.
root = auditTrailElement->FirstChildElement();
dataElement = root->FirstChildElement()->ToElement();
//get the "Data" Attribute.
//get the "活动时间" and it's value.
activeTime = dataElement->Attribute("name");
cout << activeTime.c_str() << ":";//输出"活动时间"及值.
cout << dataElement->FirstChild()->Value() << endl;
//get the "actor" and it's value
dataElement = dataElement->NextSiblingElement();
actor = dataElement->Attribute("name");
cout << actor.c_str() << ":" ;//输出"actor"及值.
cout << dataElement->FirstChild()->Value() << endl;
//get the "cost" and it's value.
dataElement = dataElement->NextSiblingElement();
if (dataElement){
cost = dataElement->Attribute("name");
cout << cost.c_str() << ":" ;//输出"cost"及值.
cout << dataElement->FirstChild()->Value() << endl;
}


//——iteratively get the "WorkflowModelElement,EventType,Timestamp,Originator" elements——.
root = root->NextSiblingElement();//指向"WorkflowModelElement"元素.
for (;root;root = root->NextSiblingElement())
{
cout << root->Value() << ":" << root->FirstChild()->Value() << endl;
}
}
}



//--------------------------------------------循环----------------------------------------------------




/*
//get the "ProcessInstance" element.
/ *
root = processElement->FirstChild();//root 现在为第一个ProcessInstance.
processInstanceElement = root->ToElement();//processInstanceElement指向root中元素.
* /
processInstanceElement = processElement->FirstChild()->ToElement();
//get the "ProcessInstance" Attribute
processInstanceId = processInstanceElement->Attribute("id");//获取caseID.
//cout << "ProcessInstance id:" << processInstanceId.c_str() << endl;//输出"ProcessInstance id"的值.


//------------------------------------------------
//   iteratively get "AuditTrailEntry" elements.
//------------------------------------------------
//get the first "AuditTrailEntry" element.
root = processInstanceElement->FirstChildElement();
auditTrailElement = root->ToElement();
for (;auditTrailElement;auditTrailElement = auditTrailElement->NextSiblingElement())
{
cout << "----------------------------------------" << endl;
cout << "ProcessInstance id:" << processInstanceId.c_str() << endl;//输出"ProcessInstance id"的值.
//——get the "Data" element——.
root = auditTrailElement->FirstChildElement();
dataElement = root->FirstChildElement()->ToElement();
//get the "Data" Attribute.
//get the "活动时间" and it's value.
activeTime = dataElement->Attribute("name");
cout << activeTime.c_str() << ":";//输出"活动时间"及值.
cout << dataElement->FirstChild()->Value() << endl;
//get the "actor" and it's value
dataElement = dataElement->NextSiblingElement();
actor = dataElement->Attribute("name");
cout << actor.c_str() << ":" ;//输出"actor"及值.
cout << dataElement->FirstChild()->Value() << endl;
//get the "cost" and it's value.
dataElement = dataElement->NextSiblingElement();
if (dataElement){
cost = dataElement->Attribute("name");
cout << cost.c_str() << ":" ;//输出"cost"及值.
cout << dataElement->FirstChild()->Value() << endl;
}


//——iteratively get the "WorkflowModelElement,EventType,Timestamp,Originator" elements——.
root = root->NextSiblingElement();//指向"WorkflowModelElement"元素.
for (;root;root = root->NextSiblingElement())
{
cout << root->Value() << ":" << root->FirstChild()->Value() << endl;
}
}

*/


cout << "========================================" << endl;
return 0;
}

你可能感兴趣的:(读取XML文件信息)