TinyXml解析配置文件

TinyXml to parse conf file

本文关键字:配置文件 TinyXml      更新时间:2023-10-16

我正在尝试实现如何使用TinyXML库。

我必须解析这个配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<Client>
    <port  num = "20035">
    <server_addr ip="127.0.0.1">    
        <AV_list>
                <AV>
                        <AVNAME>BitDefender</AVNAME>>
                        <AVPATH> C:Program FilesCommon FilesBitDefenderBitDefender Threat Scannerav64bit_26308bdc.exe </AVPATH>
                        <AVMASK>0x80000000</AVMASK>
                        <AVCOMMANDLINE> %avpath% log=%avlog% %scanpath% </AVCOMMANDLINE>
                        <AVREGEX>(%scanpath%.*?)+(([a-zA-Z0-9]+\.)+[a-zA-Z]{2,4})+(.+[a-zA-Z_])</AVREGEX>
                        <AVLOG>C:logbd_log.txt</AVLOG>
                </AV>
        </AV_list>  
</Client>

和c++代码

#include "stdafx.h"
#include "iostream"
#include "tinyxml.h"
int main(int argc, char* argv[])
{
    TiXmlDocument doc( "D:\client_conf.xml" );
    bool loadOkay = doc.LoadFile();
    if ( loadOkay )  
        printf("Yes n");
    else
        printf("No n");
    TiXmlHandle hDoc(&doc);
    TiXmlElement* pElem;
    TiXmlText* pText;
    TiXmlHandle hRoot(0);
    pElem = hDoc.FirstChildElement().Element();    
    if (!pElem)
     printf("error element");
    hRoot = TiXmlHandle(pElem);
    pElem = hRoot.FirstChild("server_addr").Element();
    const char* info = pElem->Attribute("ip");
    printf(  "%s n", info);    
    pElem = hRoot.FirstChild("port").Element();
    info = pElem->Attribute("num");
    printf( "%s n", info); 
    system("pause");
    return 0;
}

现在我可以得到前两个参数,但不知道如何达到"AV_list"块。任何帮助都将不胜感激。(:

请看一下TinyXml文档。您的朋友是TiXmlNode类引用。您也可以在TiXmlElements上使用大多数Node函数。您已经使用了FirstChild()函数来获取元素的第一个子元素;使用NextSibling()函数遍历所有元素。您也可以使用NextSiblingElement()函数直接获取元素。

另一个更复杂的解决方案是使用XPath从xml文件中检索元素。TinyXPath是建立在TinyXML之上的。它需要一些XPath知识,但可能是值得的。(XPath标准)