diff --git a/tinyxml2.h b/tinyxml2.h index 39dd12ec..4cc694b6 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1365,6 +1365,17 @@ class TINYXML2_LIB XMLElement : public XMLNode return a->QueryFloatValue( value ); } + /// See QueryIntAttribute() + XMLError QueryStringAttribute(const char* name, const char** value) const { + const XMLAttribute* a = FindAttribute(name); + if (!a) { + return XML_NO_ATTRIBUTE; + } + *value = a->Value(); + return XML_SUCCESS; + } + + /** Given an attribute name, QueryAttribute() returns XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion diff --git a/xmltest.cpp b/xmltest.cpp index c8b0181a..a647e633 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -831,6 +831,14 @@ int main( int argc, const char ** argv ) XMLTest("Attribute: unsigned", (int)XML_SUCCESS, queryResult, true); XMLTest("Attribute: unsigned", unsigned(100), v, true); } + { + const char* v = "failed"; + int queryResult = element->QueryStringAttribute("not-attrib", &v); + XMLTest("Attribute: string default", false, queryResult == XML_SUCCESS); + queryResult = element->QueryStringAttribute("attrib", &v); + XMLTest("Attribute: string", (int)XML_SUCCESS, queryResult, true); + XMLTest("Attribute: string", "100", v); + } XMLTest("Attribute: unsigned", unsigned(100), element->UnsignedAttribute("attrib"), true); } {