diff --git a/modules/c++/xml.lite/include/xml/lite/Element.h b/modules/c++/xml.lite/include/xml/lite/Element.h index 866c8d2f3..0e5020009 100644 --- a/modules/c++/xml.lite/include/xml/lite/Element.h +++ b/modules/c++/xml.lite/include/xml/lite/Element.h @@ -459,6 +459,36 @@ class Element void depthPrint(io::OutputStream& stream, const string_encoding*, int depth, const std::string& formatter) const; }; + +/*! + * Returns the character data of this element converted to the specified type. + * \param value the charater data as T + * \return whether or not there was a value of type T + */ +template +inline bool getValue(const Element& element, T& value) +{ + const auto characterData = element.getCharacterData(); + if (characterData.empty()) + { + return false; // call getCharacterData() to get an empty string + } + + value = str::toType(characterData); + return true; +} + + +/*! + * Sets the character data for this element by calling str::toString() on the value. + * \param value The data to add to this element + */ +template +inline void setValue(Element& element, const T& value) +{ + element.setCharacterData(str::toString(value)); +} + } } diff --git a/modules/c++/xml.lite/unittests/test_xmlelement.cpp b/modules/c++/xml.lite/unittests/test_xmlelement.cpp index 18fc1cc93..bac285882 100644 --- a/modules/c++/xml.lite/unittests/test_xmlelement.cpp +++ b/modules/c++/xml.lite/unittests/test_xmlelement.cpp @@ -27,8 +27,19 @@ #include "xml/lite/MinidomParser.h" -static const std::string text("TEXT"); -static const std::string strXml = "" + text + ""; +static const std::string text = "TEXT"; +static const std::string strXml1_ = R"( + + + )"; +static const std::string strXml2_ = R"( + + 314 + 3.14 + 314 + +)"; +static const auto strXml = strXml1_ + text + strXml2_; struct test_MinidomParser final { @@ -161,6 +172,65 @@ TEST_CASE(test_getElementByTagName_b) TEST_SPECIFIC_EXCEPTION(doc.getElementByTagName("b"), xml::lite::XMLException); } +TEST_CASE(test_getValue) +{ + test_MinidomParser xmlParser; + const auto root = xmlParser.getRootElement(); + + { + const auto& e = root->getElementByTagName("int", true /*recurse*/); + int value; + const auto result = getValue(e, value); + TEST_ASSERT_TRUE(result); + TEST_ASSERT_EQ(314, value); + } + { + const auto& e = root->getElementByTagName("double", true /*recurse*/); + double value; + const auto result = getValue(e, value); + TEST_ASSERT_TRUE(result); + TEST_ASSERT_EQ(3.14, value); + } + { + const auto& e = root->getElementByTagName("string", true /*recurse*/); + std::string value; + const auto result = getValue(e, value); + TEST_ASSERT_TRUE(result); + TEST_ASSERT_EQ("314", value); + } +} + +TEST_CASE(test_setValue) +{ + test_MinidomParser xmlParser; + auto root = xmlParser.getRootElement(); + + { + auto& e = root->getElementByTagName("int", true /*recurse*/); + setValue(e, 123); + int value; + const auto result = getValue(e, value); + TEST_ASSERT_TRUE(result); + TEST_ASSERT_EQ(123, value); + } + { + auto& e = root->getElementByTagName("double", true /*recurse*/); + setValue(e, 1.23); + double value; + const auto result = getValue(e, value); + TEST_ASSERT_TRUE(result); + TEST_ASSERT_EQ(1.23, value); + } + { + auto& e = root->getElementByTagName("string", true /*recurse*/); + setValue(e, "123"); + std::string value; + const auto result = getValue(e, value); + TEST_ASSERT_TRUE(result); + TEST_ASSERT_EQ("123", value); + } +} + int main(int, char**) { TEST_CHECK(test_getRootElement); @@ -169,4 +239,7 @@ int main(int, char**) TEST_CHECK(test_getElementByTagName); TEST_CHECK(test_getElementByTagName_nothrow); TEST_CHECK(test_getElementByTagName_b); + + TEST_CHECK(test_getValue); + TEST_CHECK(test_setValue); }