tinyxml怎么找到第二个子节点(有名字一样的节点)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/vv1025/article/details/102756079
void CXmlTestDlg::OnBnClickedOk()
{
    TCHAR appPath[MAX_PATH+1];
    GetModuleFileName(NULL,appPath,MAX_PATH);
    CString str_Path=appPath;
    int pos = str_Path.ReverseFind('\\');
    CString m_File = str_Path.Left(pos+1)+_T("config.xml");

    char xmlFile[256];
    wcstombs(xmlFile,m_File,256);
    TiXmlDocument doc(xmlFile);
    bool loadOkay = doc.LoadFile();
    if (!loadOkay)
    {
        AfxMessageBox(_T("Load failed!"));
        return;
    }
    setlocale( LC_CTYPE, "chs" );

    CharsChange strChange;
    TiXmlNode* node = 0;
    TiXmlNode*nextnode=0;
    TiXmlElement* todoElement = 0;
    node = doc.FirstChild( "system" );
    if (0==node)
        return;
    if (nextnode=node->FirstChild("fontname"))
    {
        todoElement=nextnode->ToElement();
        CString strGet=strChange.doChange(todoElement->GetText()).c_str();
        TRACE(strGet);
    }
    if (nextnode=node->FirstChild("tinyfontsize"))
    {
        todoElement=nextnode->ToElement();
        //    sysEntry.tinyfontsize=8;
        int x;
        todoElement->QueryIntAttribute("size",&x);
        CString strX;
        strX.Format(L"%d",x);
        TRACE(strX);
    }
    if (nextnode=node->FirstChild("backfile"))
    {
        todoElement=nextnode->ToElement();
        CString strGet=strChange.doChange(todoElement->GetText()).c_str();
        TRACE(strGet);
        /*    sysEntry.background=new ImageWrap;
            if(false==sysEntry.background->loadFile(strChange.doChange(todoElement->GetText()).c_str()))
            {
                delete sysEntry.background;
                sysEntry.background=0;
            }*/
    }
    if (nextnode=node->FirstChild("window"))
    {
        todoElement=nextnode->ToElement();
        //    sysEntry.minWindowSize.cx=640;
        //sysEntry.minWindowSize.cy=480;
        int value;
        todoElement->QueryIntAttribute("minWidth",&value);
        //    sysEntry.minWindowSize.cx=value;
        todoElement->QueryIntAttribute("minHeight",&value);
        //sysEntry.minWindowSize.cy=value;
    }
    if (nextnode=node->FirstChild("adjuststatic"))
    {
        TiXmlNode* childNode=0;
        CString strValue;
        if (childNode=nextnode->FirstChild("static"))
        {
            todoElement=childNode->ToElement();
            //    int value;
            std::string value;
            todoElement->QueryValueAttribute("name",&value);
            strValue=strChange.doChange(value).c_str();
            AfxMessageBox(strValue);

            todoElement->QueryValueAttribute("bmplogo",&value);
            strValue=strChange.doChange(value).c_str();
            AfxMessageBox(strValue);
        }
        if (childNode=nextnode->NextSibling("static"))//这里得不到第二个static?????????????????????????????
        {
            todoElement=childNode->ToElement();
            //    int value;
            std::string value;
            todoElement->QueryValueAttribute("name",&value);
            strValue=strChange.doChange(value).c_str();
            AfxMessageBox(strValue);

            todoElement->QueryValueAttribute("bmplogo",&value);
            strValue=strChange.doChange(value).c_str();
            AfxMessageBox(strValue);
        }
    }

}

tinyxml里FirstChild是返回被选节点的第一个子节点,如nextnode->FirstChild("static")),就找到了<static>,如果要找到第二个<static>,要怎么写?

NextSibling方法。

试试childNode->NextSibling("static")

谢谢大家,解决了!
不过遇到难题了,在wince下,2006年版的tinyxml出现错误,提示找不到FirstChild等函数

wince下重新编译后,出现的问题是
1>.\tinyxml.cpp(43) : error C3861: 'fopen_s': identifier not found
1>.\tinyxml.cpp(115) : error C3861: '_snprintf_s': identifier not found
1>.\tinyxml.cpp(688) : error C3861: '_snprintf_s': identifier not found
1>.\tinyxml.cpp(710) : error C3861: '_snprintf_s': identifier not found
1>.\tinyxml.cpp(1243) : error C3861: 'sscanf_s': identifier not found
1>.\tinyxml.cpp(1250) : error C3861: 'sscanf_s': identifier not found
1>.\tinyxml.cpp(1259) : error C3861: '_snprintf_s': identifier not found
1>.\tinyxml.cpp(1270) : error C3861: '_snprintf_s': identifier not found
哪位高手知道,要怎么解决啊?

引用 8 楼 arabicsara 的回复:

wince下重新编译后,出现的问题是
1>.\tinyxml.cpp(43) : error C3861: 'fopen_s': identifier not found
1>.\tinyxml.cpp(115) : error C3861: '_snprintf_s': identifier not found
1>.\tinyxml.cpp(688) : error C3861: '_……



include
<stdio.h> and <wchar.h>

如果XML文件不大的话,可以考虑纯文本 正则字符串处理

引用 8 楼 arabicsara 的回复:

wince下重新编译后,出现的问题是
1>.\tinyxml.cpp(43) : error C3861: 'fopen_s': identifier not found
1>.\tinyxml.cpp(115) : error C3861: '_snprintf_s': identifier not found
1>.\tinyxml.cpp(688) : error C3861: '_……

解决方法:
1. 在工程->属性->C++->Preprocessor->Preprocessor Definition里面添加"_LIB;"
如果想用STL的TinyXML的话,添加上TIXML_USE_STL
2. 在tinystr.h和tinyxml.h里面第一行添加:
#define _MSC_VER        1
这样TinyXML就会调用C标准库了,不会使用CRT. 

猜你喜欢

转载自blog.csdn.net/vv1025/article/details/102756079