zoukankan      html  css  js  c++  java
  • tinyxml安装和使用

    下载:

    官方文档:http://www.grinninglizard.com/tinyxmldocs/index.html

    中文翻译:http://www.cnblogs.com/kex1n/archive/2010/10/03/1841502.html

    TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。

    DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。

    引用来自tinyXML文档

    iXmlBase是所有类的基类,TiXmlNode、TiXmlAttribute两个类都继承来自TiXmlBase类,其中TiXmlNode类指的是所有被<...>...<.../>包括的内容,而xml中的节点又具体分为以下几方面内容,分别是声明、注释、节点以及节点间的文本,因此在TiXmlNode的基础上又衍生出这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown,分别用来指明具体是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号里面的内容,像<... ***=...>,其中***就是一个属性。

         Tinyxml使用了两种编译选择:使用标准C的char *类型或者使用STL中的std::string,其中使用预处理器TIXML_USE_STL进行控制,即添加了TIXML_USE_STL为使用std::string的。鉴于STL的广泛使用以及其强大功能,下面我以使用std::string的tinyxml说明。

    首先使用VS 2005打开tinyxmlSTL.dsp的工程文件,将其编译成一个静态库,debug版本为:tinyxmld_STL.lib,然后开始测试tinyxml库。我的测试计划是这样的:首先使用tinyxml库创建example4.xml,然后将其读出来,然后查询指定节点的属性或文本,再修改example4.xml(修改其中的一些节点值和删除其中一个节点,增加一个节点),然后再读出来以判断是否修改成功。具体是在VS 2005上新建一个控制台工程:Test,注意使用多字节字符集进行编译,同时添加。首先是创建xml文件的代码:

    /*!
    *  /brief 创建xml文件。
    *
    *  /param XmlFile xml文件全路径。
    *  /return 是否成功。true为成功,false表示失败。
    */
    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;
    }&nbsp;
    View Code

           不知你注意到上面的规律没有?首先父节点连接字节点使用函数LinkEndChild,使用方法是:pParentNode-> LinkEndChild(pChild);其次设置类似这种结构<Window name="MainFrame" x="5" y="15" w="400" h="250" />采用SetAttribute函数,这个函数有两个参数,前一个参数表示键,后一个参数表示键值,设置<Farewell>Thank you for using MyApp</Farewell>这种结构采用TiXmlText类,使用LinkEndChild函数进行连结。

          上面是创建xml文件的代码,下面介绍读取xml文件的代码。打印整个xml文件的代码很简单,代码如下:

    /*!
    *  /brief 打印xml文件。
    *
    *  /param XmlFile xml文件全路径。
    *  /return 是否成功。true为成功,false表示失败。
    */
    bool PaintXml(std::string XmlFile)
    {
        // 定义一个TiXmlDocument类指针
        TiXmlDocument *pDoc = new TiXmlDocument();
        if (NULL==pDoc)
        {
            return false;
        }
        pDoc->LoadFile(XmlFile);
        pDoc->Print();
        return true;
    }

    Handles

    Where browsing an XML document in a robust way, it is important to check for null returns from method calls. An error safe implementation can generate a lot of code like:

    TiXmlElement* root = document.FirstChildElement( "Document" );
    if ( root )
    {
    	TiXmlElement* element = root->FirstChildElement( "Element" );
    	if ( element )
    	{
    		TiXmlElement* child = element->FirstChildElement( "Child" );
    		if ( child )
    		{
    			TiXmlElement* child2 = child->NextSiblingElement( "Child" );
    			if ( child2 )
    			{
    				// Finally do something useful.
    

    Handles have been introduced to clean this up. Using the TiXmlHandle class, the previous code reduces to:

    TiXmlHandle docHandle( &document );
    TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).ToElement();
    if ( child2 )
    {
    	// do something useful
    

    Which is much easier to deal with. See TiXmlHandle for more information.

    xml文档;

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <SearchMode ModeCount="3">
        <Mode1 Name="Numbers" RECount="2" Space="110">
            <RE1>[0-9]{1,1}</RE1>
            <RE2>[0-9]{2,2}</RE2>
        </Mode1>
        <Mode2 Name="Lower_Case" RECount="3" Space="12356845">
            <RE1>[a-z]{3,3}</RE1>
            <RE2>[a-z]{4,4}</RE2>
            <RE3>[a-z]{5,5}</RE3>
        </Mode2>
        <Mode3 Name="Lower_Case_Num" RECount="4" Space="554433442">
            <RE1>[a-z0-9]{6,6}</RE1>
            <RE2>[a-z0-9]{7,7}</RE2>
            <RE3>[a-z0-9]{8,8}</RE3>
            <RE4>[a-z0-9]{9,9}</RE4>
        </Mode3>
    </SearchMode>
     
    bool GetNodePointerByName(TiXmlElement* pRootEle, const char* strNodeName, TiXmlElement* &destNode)
    {  //根据结点名获取结点指针  
        // 假如等于根节点名,就退出   
        if (0 == strcmp(strNodeName, pRootEle->Value()))
        {
            destNode = pRootEle;
            return true;
        }
    
        TiXmlElement* pEle = pRootEle;
        for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
        {
            // 递归处理子节点,获取节点指针      
            if (0 != strcmp(pEle->Value(), strNodeName))
            {
                GetNodePointerByName(pEle, strNodeName, destNode);
            }
            else
            {
                destNode = pEle;
                printf("destination node name: %s
    ", pEle->Value());
                return true;
            }
        }
        return false;
    }
     
    int readXmlFile()
    {
        TiXmlDocument *pDoc = new TiXmlDocument();
        pDoc->LoadFile("xmlData.xml");
        TiXmlElement *RootElem = pDoc->RootElement();//root elem
    
        TiXmlElement *pEle = NULL;
    
        GetNodePointerByName(RootElem, "SearchMode", pEle);//找到值为searchmodes的节点
        if (!pEle)
        {
            cout << "can't find it" << endl;
            return 0;
        }
        else
            cout << "find it!" << endl;
        //遍历该节点
        for (TiXmlElement *curEle = pEle->FirstChildElement(); curEle; curEle = curEle->NextSiblingElement())
        {
            cout << "node:value:" << curEle->Value() << " ";
            
            TiXmlAttribute *pAttr = curEle->FirstAttribute();
            while (pAttr) //输出所有属性
            {
                cout << pAttr->Name() << ":" << pAttr->Value() << " ";
                pAttr = pAttr->Next();
            }
    
            cout << endl;
         
            for (TiXmlElement *regExElem = curEle->FirstChildElement(); regExElem; regExElem->NextSiblingElement()) //输出子元素的值
            {
                cout <<regExElem->FirstChild()->Value() << endl; 这块有问题,死循环
            }
         
        }
        delete pDoc;
        return 1;
    }  

    attr = elem->Attribute("priority");获取某个属性

    参考:http://blog.csdn.net/L_Andy/article/details/40615517
    http://blog.csdn.net/mjay1234/article/details/7380536
  • 相关阅读:
    Linux shell的&&和||--转载
    Paxos算法之旅(四)zookeeper代码解析--转载
    linux shell less 命令---转
    8 Pratical Examples of Linux “Touch” Command--reference
    5 Ways to Use Log Data to Analyze System Performance--reference
    Load resources from classpath in Java--reference
    Eclipse Key Shortcuts for Greater Developers Productivity--reference
    binary heap
    Nginx vs Apache--reference
    【Java技术位】——代理模式及其事务包
  • 原文地址:https://www.cnblogs.com/youxin/p/5131693.html
Copyright © 2011-2022 走看看