Menu

HasAttribute() extension...

Developer
nfactorial
2003-02-14
2003-03-18
  • nfactorial

    nfactorial - 2003-02-14

    Hiya, I dloaded tinyxml last night for a research project. In my own source copy I've added the following extensions to the TiXmlElement class (within tinyxml.h):

    bool HasAttribute( const char* name ) const
    {
        return ( NULL != Attribute( name ) );
    }

    bool HasAttribute( const char* name, int* i ) const
    {
        return ( NULL != Attribute( name, i ) );
    }

    /// Inside the #ifdef TIXML_USE_STL section...
    bool HasAttribute( const std::string& name ) const
    {
        return ( NULL != Attribute( name ) );
    }

    bool HasAttribute( const std::string& name, int* i ) const
    {
        return ( NULL != Attribute( name, i ) );
    }

    I've found these useful in my current project, and maybe it would be useful within the codebase?
    Sorry, I'm very new to source-forge so I don't know whether (or even how :) to add these myself.

    Maybe they seem pointless to people more used to xml?

    Also a quick question, why doesn't tinyxml use namespaces?

    Anyway, thanks and it's a great lib. Compiled first time in my project which was unexpected but impressive :)

    n!

     
    • B Sizer

      B Sizer - 2003-03-18

      Well, I don't think most people mind checking for a NULL return, but it's up to you :)  I'd be a bit worried about passing in the int* though... if you're just checking for the presence of an attribute, why store the value as well?

      An alternative implementation might be:

      bool HasAttribute( const char * name ) const
      {
          return attributeSet.Find( name ) != 0;
      }

      And obviously the same sort of thing for std::strings. This cuts out the 'middleman' of the 'Attribute()' ' function, for what little it's worth.

       

Log in to post a comment.

MongoDB Logo MongoDB