Skip to content

Commit

Permalink
get & set the characer data as a type
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Dec 22, 2020
1 parent f3ee1ee commit a848aa3
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
30 changes: 30 additions & 0 deletions modules/c++/xml.lite/include/xml/lite/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
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<T>(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 <typename T>
inline void setValue(Element& element, const T& value)
{
element.setCharacterData(str::toString(value));
}

}
}

Expand Down
77 changes: 75 additions & 2 deletions modules/c++/xml.lite/unittests/test_xmlelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@

#include "xml/lite/MinidomParser.h"

static const std::string text("TEXT");
static const std::string strXml = "<root><doc><a>" + text + "</a><b/><b/></doc></root>";
static const std::string text = "TEXT";
static const std::string strXml1_ = R"(
<root>
<doc name="doc">
<a a="a">)";
static const std::string strXml2_ = R"(</a><b/><b/>
<values int="314" double="3.14" string="314"/>
<int>314</int>
<double>3.14</double>
<string>314</string>
</doc>
</root>)";
static const auto strXml = strXml1_ + text + strXml2_;

struct test_MinidomParser final
{
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

0 comments on commit a848aa3

Please sign in to comment.