c++ 使用TinyXML 读取xml数据
2014-08-15 20:05
519 查看
1 TinyXml 下载地址 点击打开链接
2 下载完成后解压缩 将tinystr.cpp ,tinystr.h,tinyxml.cpp,tinyxml.h,tinyxmlerror.cpp,tinyxmlparser.cpp 加入工程中
3
2 下载完成后解压缩 将tinystr.cpp ,tinystr.h,tinyxml.cpp,tinyxml.h,tinyxmlerror.cpp,tinyxmlparser.cpp 加入工程中
3
#include<iostream> #include "tinystr.h" #include "tinyxml.h" #include<string> #include<windows.h> using namespace std; #define MAX_PATH1 100 /*获取exe文件路径*/ string GetAppPath() { char spath[MAX_PATH1] = {0}; GetModuleFileName(NULL,spath,MAX_PATH); string path(spath); cout<<path<<endl; int m= path.find_last_of('\\'); path = path.substr(0,m); cout<<path<<endl; return path; } /*创建一个xml数据*/ bool CreateXmlFile(string& szFileName) { try { TiXmlDocument *myDocument = new TiXmlDocument(); TiXmlElement *RootElement = new TiXmlElement("Persons"); myDocument->LinkEndChild(RootElement); TiXmlElement *PersonElement = new TiXmlElement("Person"); RootElement->LinkEndChild(PersonElement); PersonElement->SetAttribute("ID",1); TiXmlElement *PersonName = new TiXmlElement("name"); TiXmlElement *PersonAge = new TiXmlElement("age"); PersonElement->LinkEndChild(PersonName); PersonElement->LinkEndChild(PersonAge); TiXmlText *NameContent = new TiXmlText("小家"); TiXmlText *AgeContent = new TiXmlText("23"); PersonName->LinkEndChild(NameContent); PersonAge->LinkEndChild(AgeContent); string spath = GetAppPath(); spath += "\\"; spath += szFileName; myDocument->SaveFile(spath.c_str()); } catch (string e) { return false; } return true; } bool ReadXmlFile(string& szFileName) { try { string apppath = GetAppPath(); apppath += "\\"; apppath += szFileName; TiXmlDocument *myDocument = new TiXmlDocument(apppath.c_str()); myDocument->LoadFile(); TiXmlElement *RootElement = myDocument->RootElement(); cout<<RootElement->Value()<<endl; TiXmlElement *FirstPerson = RootElement->FirstChildElement(); TiXmlElement *NameElement = FirstPerson->FirstChildElement(); TiXmlElement *AgeElement = NameElement->NextSiblingElement(); TiXmlAttribute *personattri = FirstPerson->FirstAttribute(); cout<<NameElement->FirstChild()->Value()<<endl; cout<<AgeElement->FirstChild()->Value()<<endl; cout<<personattri->Value()<<endl; } catch(string e) { return false; } return true; } int main() { string str = "123.txt"; if(CreateXmlFile(str)) cout<<"ok"<<endl; else cout<<"0"<<endl; if(ReadXmlFile(str)) cout<<"ok"<<endl; else cout<<"0"<<endl; return 0; }
相关文章推荐
- 毕业论文中使用的XML读取和制造工具!!!C++ 使用TinyXML解析XML文件
- C++读取XML,tinyXml的使用
- C++使用tinyXml读取XML
- C++使用tinyXml读取XML
- C++读取XML,tinyXml的使用
- C++读取XML,tinyXml的使用
- 在.NET中使用XML、读取XML文件,插入数据到XML文档中。
- 接上回,flash组件使用读取XML数据
- [libexpat]_[XML处理]_[C/C++使用libexpat库以数据流(SAX模型)的方式读取(解析)大XML文件]
- 使用dom4j读取xml文件初始化数据
- 使用ClientDataSet读取XML数据,并建立主从报表
- C++生成xml(使用tinyxml)
- Extjs GridPanel用XmlReader读取xml文件的数据 及mapping的使用规则
- ZendFramework学习第三章(核心组件—使用配置数据之从XML配置文件中读取数据)
- 读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。【轉】
- 在.NET中使用XML、读取XML文件,插入数据到XML文档中。
- WCF:读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。
- C#不使用DataSet操作XML,XmlDocument读写xml所有节点及读取xml节点的数据总结
- c++ 使用TinyXml读写Xml
- 读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。