<?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; }
#define TIXML_USE_STL
#pragma comment(lib, "tinyxmlSTL.lib")