XML文件系统

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三个方面的数据信息以及相关的操作。(<?xml version=”1.0″ standalone=no>)


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;
}

三、参考学习博客:

https://www.cnblogs.com/hgwang/p/5833638.html

https://blog.csdn.net/xuanyin235/article/details/81022807

原文地址:https://www.cnblogs.com/retry/p/9330872.html