简体   繁体   中英

TinyXML2 returns Null?

I'm currently embedding TinyXML2 into a game engine. I have this code, which in theory should work unless I have been staring at it for too long. Can somebody offer any help? Much appreciated!

Basically it returns Null.

const char *CXMLManager::GetWeaponGeometry( const char *pWeaponFile, const char *pParam )
{
    LoadWeaponXML( pWeaponFile );

    const char *pData;

    XMLElement* root = doc.FirstChildElement()->FirstChildElement( "geometry" );
    for(XMLElement* e = root->FirstChildElement("param"); e != NULL; e = e->NextSiblingElement("param"))
    {
        pData = e->Attribute( pParam );
    }

    return pData;
}

And here is my XML setup.

<weapondata>
    <param bullet_damage='2' />
    <param melee_damage='42' />
    <geometry>
        <param fp_mesh='models/weapons/v_pist_deagle.mdl' />
        <param tp_mesh='models/weapons/w_pist_deagle.mdl' />
    </geometry>
</weapondata>

You're digging too deep in the beginning, remove one of the FirstChildElement calls. And then in the iteration of param sub-elements, you seem to always return the last one, I've changed so that the first param with the requested attribute is returned.

const char *CXMLManager::GetWeaponGeometry( const char *pWeaponFile, const char *pParam )
{
    LoadWeaponXML( pWeaponFile );
    if (XMLElement* root = doc.FirstChildElement( "geometry" ))
    {
        for (XMLElement* e = root->FirstChildElement("param");
             e != NULL;
             e = e->NextSiblingElement("param"))
        {
            if (const char *pData = e->Attribute( pParam ))
                return pData;
        }
    }

    return NULL;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM