xml解析实例

// 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值