XML文件系统
一、TinyXml Object Model简介:
1.tixmlBase: 所有tinyxml中的对象的公共基类,实现了一些公共的操作,比如字符编码转换等,另外还定义了一些公共的数据结构,比如错误类型等。
2.tixmlNode: 是DOM树中结点元素的基类型,它定义了DOM树结点的一些特征数据以及一些相关的操作。
3.tixmlDocument:对应于XML文档整体的一个对象,一棵DOM的根结点是tixmlDocument类型,而且基它结点不能为
4.tixmlDocument类型。(example.xml)
5.tixmlDeclaration:对应于XML文档开始部分声明部分的对象,它主要包含version,encode,standalone三个方面的数据信息以及相关的操作。()
6.tixmlComment: 对应于XML文档中的注释部分的对象,它主要包含注释的内容以及相关的操作。(<!– Our to do list data –> )
7.tixmlElement:对应于XML文档中普通的元素的对象,每个元素有一个对应的名字,另外还可以有一些属性。tixmlElement包含了这些相关信息及其操作。 (
8.tixmlText: 对应于XML文档中元素中的文本信息的对象,它实现了文本信息相关的操作。(Go to the , Toy store!, Do bills)
9.tixmlAttributeSet: XML文档中某个元素的所有属性的集合,它是tixmlElement的一部分,它用来管理该元素的所有属性。
10.tixmlAttribute: XML文档中的元素的属性所对应的对象。它是一个name-value pair对象,name是属性名,value是属性值。
11.tixmlUnkNown: 所有的用上面的对象不能表示的内容所对应的对象。
TinyXML中,根据XML的各种元素来定义了一些类
参数 | 含义 |
---|---|
tixmlBase | 整个TinyXML模型的基类 |
tixmlAttribut | 对应于XML中的元素的属性 |
tixmlNode | 对应于DOM结构中的节点 |
tixmlComment | 对应于XML中的注释 |
tixmlDeclaration | 对应于XML中的申明部分,即<?versiong="1.0" ?> |
tixmlDocument | 对应于XML的整个文档 |
tixmlElement | 对应于XML的元素 |
tixmlText | 对应于XML的文字部分 |
tixmlUnkNown | 对应于XML的未知部分 |
tixmlHandle | 定义了针对XML的一些操作 |
二、XML示例结构
- 弄清概念:节点、元素、属性、文本、声明、注释
//示例1: <?xml version="1.0" ?> <Hello>World</Hello> //示例2: <?xml version="1.0" ?> <poetry> <verse> Alas Great World Alas (again) </verse> </poetry> //示例3: <?xml version="1.0" ?> <shapes> <circle name="int-based" x="20" y="30" r="50" /> <point name="float-based" x="3.5" y="52.1" /> </shapes> //示例4: <?xml version="1.0" ?> <MyApp> <Messages> <Welcome>Welcome to MyApp</Welcome> //<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> 总结:节点在尖括号内<window>,属性值尖括号内<Window name="MainFrame" x="5" y="15" w="400" h="250" />,文本在尖括号外Welcome to MyApp.
三、示例程序
以上述xml示例结构4为例
//生成xml文件 bool CreateXml(std::string XmlFile) { // 定义一个tixmlDocument类指针 tixmlDocument *pDoc = new tixmlDocument; if (NULL==pDoc) { return false; } tixmlDeclaration *pDeclaration = new tixmlDeclaration(_T("1.0"),_T(""),_T("")); if (NULL==pDeclaration) { return false; } pDoc->LinkEndChild(pDeclaration); // 生成一个根节点:MyApp tixmlElement *pRootEle = new tixmlElement(_T("MyApp")); if (NULL==pRootEle) { return false; } pDoc->LinkEndChild(pRootEle); // 生成子节点:Messages tixmlElement *pMsg = new tixmlElement(_T("Messages")); if (NULL==pMsg) { return false; } pRootEle->LinkEndChild(pMsg); // 生成子节点:Welcome tixmlElement *pWelcome = new tixmlElement(_T("Welcome")); if (NULL==pWelcome) { return false; } pMsg->LinkEndChild(pWelcome); // 设置Welcome节点的值 std::string strValue = _T("Welcome to MyApp"); tixmlText *pWelcomeValue = new tixmlText(strValue); pWelcome->LinkEndChild(pWelcomeValue); // 生成子节点:farewell tixmlElement *pfarewell = new tixmlElement(_T("farewell")); if (NULL==pfarewell) { return false; } pMsg->LinkEndChild(pfarewell); // 设置farewell节点的值 strValue = _T("Thank you for using MyApp"); tixmlText *pfarewellValue = new tixmlText(strValue); pfarewell->LinkEndChild(pfarewellValue); // 生成子节点:Windows tixmlElement *pWindows = new tixmlElement(_T("Windows")); if (NULL==pWindows) { return false; } pRootEle->LinkEndChild(pWindows); // 生成子节点:Window tixmlElement *pWindow = new tixmlElement(_T("Window")); if (NULL==pWindow) { return false; } pWindows->LinkEndChild(pWindow); // 设置节点Window的值 pWindow->SetAttribute(_T("name"),_T("MainFrame")); pWindow->SetAttribute(_T("x"),_T("5")); pWindow->SetAttribute(_T("y"),_T("15")); pWindow->SetAttribute(_T("w"),_T("400")); pWindow->SetAttribute(_T("h"),_T("250")); // 生成子节点:Window tixmlElement *pConnection = new tixmlElement(_T("Connection")); if (NULL==pConnection) { return false; } pRootEle->LinkEndChild(pConnection); // 设置节点Connection的值 pConnection->SetAttribute(_T("ip"),_T("192.168.0.1")); pConnection->SetAttribute(_T("timeout"),_T("123.456000")); pDoc->SaveFile(XmlFile); return true; }
//brief 打印xml文件。 bool PaintXml(std::string XmlFile) { // 定义一个tixmlDocument类指针 tixmlDocument *pDoc = new tixmlDocument(); if (NULL==pDoc) { return false; } pDoc->LoadFile(XmlFile); pDoc->Print(); return true; }
三、参考学习博客:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。