tinyxml

<?xml version="1.0" ?>  
<MyApp>  
    <Messages>  
        <Welcome>Welcome to MyApp</Welcome>  
        <Farewell>Thank you for using MyApp</Farewell>  
    </Messages>  
    <Windows>  
        <Window name="MainFrame" x="5" y="15" w="400" h="250" />  
    </Windows>  
    <Connection ip="192.168.0.1" timeout="123.456000" />  
</MyApp>  

制作一个快递查询的软件,需要处理XML数据,系统的学习下XML

#include <iostream>
#include "tinyxml.h"
#pragma comment(lib, "tinyxml.lib")
using namespace std;

void CreateXml(string XmlFile)
{
	TiXmlDocument *doc = new TiXmlDocument;

	TiXmlDeclaration *dec = new TiXmlDeclaration("1.0", "", "");
	doc->LinkEndChild(dec);

	TiXmlElement *root = new TiXmlElement("MyApp");
	doc->LinkEndChild(root);

	TiXmlElement *mess = new TiXmlElement("Messages");
	root->LinkEndChild(mess);

	TiXmlElement *welc = new TiXmlElement("Welcome");
	mess->LinkEndChild(welc);

	TiXmlText *welctext = new TiXmlText("Welcome to MyApp");
	welc->LinkEndChild(welctext);

	TiXmlElement *fare = new TiXmlElement("Farewell");
	mess->LinkEndChild(fare);

	TiXmlText *faretext = new TiXmlText("Thank you for using MyApp");
	fare->LinkEndChild(faretext);

	TiXmlElement *wind = new TiXmlElement("Windows");
	root->LinkEndChild(wind);

	TiXmlElement *win = new TiXmlElement("Window");
	wind->LinkEndChild(win);

	win->SetAttribute("name", "MainFrame");
	win->SetAttribute("x", "5");
	win->SetAttribute("y", "15");
	win->SetAttribute("w", "400");
	win->SetAttribute("h", "250");

	TiXmlElement *conn = new TiXmlElement("Connection");
	root->LinkEndChild(conn);

	conn->SetAttribute("ip", "192.168.0.1");
	conn->SetAttribute("timeout", "123.456000");

	doc->SaveFile(XmlFile.c_str());
}

void ReadXml(string XmlFile)
{
	TiXmlDocument *doc = new TiXmlDocument;
	doc->LoadFile(XmlFile.c_str());
	doc->Print();
}

int main(void)
{
	string XmlFile("text.xml");
	CreateXml(XmlFile);
	ReadXml(XmlFile);
	return 0;
}


使用STL版本则需要

#define TIXML_USE_STL
#pragma comment(lib, "tinyxmlSTL.lib")



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值