How To Use
 All Modules Pages

Sometimes you may be interested in comparing two xml files/strings directly with one another.

Sample data

static const std::string SHORT_DEMO_A =
R"(<SomeNode id="3" attrA="1.2">
<SubNodeB>0.2</SubNodeB>
</SomeNode>
)";
static const std::string SHORT_DEMO_B =
R"(<SomeNode id="3" attrA="1.8">
<SubNodeB>0.4</SubNodeB>
</SomeNode>
)";

Read sample data

pugi::xml_document docA;
pugi::xml_node rootA = TTB::TheXmlCheck()->ReadFromString(docA, SHORT_DEMO_A);
pugi::xml_document docB;
pugi::xml_node rootB = TTB::TheXmlCheck()->ReadFromString(docB, SHORT_DEMO_B);

Compare with all details

The check will find the first difference.

TTB_INFO("Compare A and B, check all entries of xml trees");
bool equal = TTB::TheXmlCheck()->Compare(docA, docB);
TTB_EXP("Difference detected");
TTB_EXP("lhs: attrA: 1.2(path: /SomeNode/attrA, pos: 1)");
TTB_EXP("rhs: attrA: 1.8(path: /SomeNode/attrA, pos: 1)");
// Optional check of return value:
TTB_CHECK_EQUAL(equal, false); // i.e. difference found

Ignore differing attribute

When ignoring the attribute the next difference is found:

TTB_INFO("\nCompare A and B, ignore attribute attrA");
TTB::TheXmlCheck()->IgnoreAttribute("attrA");
equal = TTB::TheXmlCheck()->Compare(docA, docB);
TTB_CHECK_EQUAL(equal, false); // i.e. difference found
TTB_EXP("Difference detected");
TTB_EXP("lhs: 0.2(path: /SomeNode/SubNodeB/, pos: 42)");
TTB_EXP("rhs: 0.4(path: /SomeNode/SubNodeB/, pos: 42)");

Ignore differing attribute and node

When ignoring both attribute and differing node the compare will finally ssucceed:

TTB_INFO("\nCompare A and B, ignore attribute attrA and node SubNodeB");
TTB::TheXmlCheck()->IgnoreNode("SubNodeB");
equal = TTB::TheXmlCheck()->Compare(docA, docB);
TTB_CHECK_EQUAL(equal, true); // i.e. no difference found