// TestXml.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <windows.h> #include "tinyxml.h" #include "lang.h" #include <string> using namespace std; using namespace std; #pragma comment(lib, "tinyxmld.lib") BOOL LoadXml(TiXmlDocument&doc, LPCTSTR lpszSrcPath); BOOL SaveXml(TiXmlDocument&doc, LPCTSTR lpszDstPath); BOOL GetDeclare(TiXmlDocument&doc); BOOL ReadXmlText(TiXmlDocument&doc); BOOL ParseXmlText(TiXmlElement* pEleRoot); BOOL ReadXmlAttr(TiXmlDocument&doc); BOOL ParseXmlAttr(TiXmlElement* pEleRoot); BOOL AddNode(TiXmlDocument& doc); BOOL DelNode(TiXmlDocument& doc); int _tmain(int argc, _TCHAR* argv[]) { TiXmlDocument doc; //load xml if (!LoadXml(doc, "./test_copy.xml")) { return -1; } //get declare GetDeclare(doc); //add and del DelNode(doc); AddNode(doc); cout << "============================Read Text==========================" << endl; //read text ReadXmlText(doc); cout << "============================Read Attribute==========================" << endl; //read attribute ReadXmlAttr(doc); if (!SaveXml(doc, "./test2.xml")) { return -1; } return 0; } BOOL LoadXml(TiXmlDocument&doc, LPCTSTR lpszSrcPath) { //load file if (!doc.LoadFile(lpszSrcPath)) { cout << "load xml failed!" << endl; return FALSE; } //print doc.Print(); return TRUE; } BOOL SaveXml(TiXmlDocument&doc, LPCTSTR lpszDstPath) { //save file if (!doc.SaveFile(lpszDstPath)) { cout << "save xml failed!" << endl; } else { cout << "save xml succ!" << endl; } return TRUE; } BOOL GetDeclare(TiXmlDocument&doc) { TiXmlNode* pXmlFirst = doc.FirstChild(); if (NULL != pXmlFirst) { TiXmlString str; TiXmlDeclaration* pXmlDec = pXmlFirst->ToDeclaration(); if (NULL == pXmlDec) { pXmlDec->Print(0, 1, &str); //cout << "szDecInfo:" << str << endl; cout << "szVer:" << pXmlDec->Version() << " is absolute:" << pXmlDec->Standalone() << "code type:" << pXmlDec->Encoding() << endl; } return TRUE; } return FALSE; } BOOL ReadXmlText(TiXmlDocument&doc) { //get root pointer TiXmlElement* pXmlRoot = doc.RootElement(); if (NULL == pXmlRoot) { return FALSE; } TiXmlNode* pParent = pXmlRoot->Parent(); TiXmlNode* pChild = pXmlRoot->FirstChild(); return ParseXmlText(pXmlRoot); } BOOL ParseXmlText(TiXmlElement* pEleRoot) { if (NULL == pEleRoot) { return FALSE; } TiXmlElement* pEle = NULL; for (pEle = pEleRoot->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement()) { cout << "/t"; //递归处理 ParseXmlText(pEle); const char* pszText = pEle->GetText(); if (NULL != pszText) { cout << pszText; } cout << endl; } return TRUE; } BOOL ReadXmlAttr(TiXmlDocument&doc) { //get root TiXmlElement* pEleRoot = doc.RootElement(); if (NULL == pEleRoot) { return FALSE; } return ParseXmlAttr(pEleRoot); } BOOL ParseXmlAttr(TiXmlElement* pEleRoot) { if (NULL == pEleRoot) { return FALSE; } TiXmlElement* pEle = pEleRoot; TiXmlAttribute* pAttr = NULL; cout << "/t"; //print owner info cout << "node:" << pEle->Value(); for (pAttr = pEle->FirstAttribute(); pAttr; pAttr = pAttr->Next()) { cout << "/t"; cout << "attr:" << pAttr->Name() << "/t val:" << pAttr->Value() << "/t"; } cout << endl; for (pEle = pEleRoot->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement()) { cout << "/t"; //递归处理子节点 ParseXmlAttr(pEle); } return TRUE; } BOOL AddNode(TiXmlDocument& doc) { TiXmlElement node("aaa"); node.SetValue("add node"); doc.InsertEndChild(node); return TRUE; } BOOL DelNode(TiXmlDocument& doc) { doc.Clear(); return TRUE; }