bool Test::testCase2() const { if (m_verbose) cout << "\tCase 2: Parse and print\n"; string text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<element1 a=\"1\" b=\"2\">\n" "\t<element2 c=\"3\" d=\"true\"/>\n" "\t<element3>Hello World!</element3>\n" "\t<element4 a=\"123\">\n" "\t\t<element5 yes=\"no\">\n" "\t\t\t<element6 str=\"This is a string\" num=\"4.5\"/>\n" "\t\t\t<element7>99.8</element7>\n" "\t\t</element5>\n" "\t</element4>\n" "</element1>\n\n"; XmlDocument doc; doc.parse(text.data(), text.length()); string output; doc.print(output); bool pass = output.compare(text) == 0; if (m_verbose) cout << (pass ? "\tPASS\n" : "\tFAIL\n"); return pass; }
bool Test::testCase0() const { bool pass = true; if (m_verbose) cout << "\tCASE 0: Parsing bad document\n"; try { XmlDocument doc; doc.parse("./test0.xml"); pass = false; } catch (XmlException& e) {} if (m_verbose) cout << (pass ? "\tPASS\n" : "\tFAIL\n"); return pass; }
int main() { XmlDocument doc; doc.parse("<?xml version='1.0' encoding='utf-8' ?><command><user>abc</user><pwd>密码</pwd></command>"); //doc.parse("<command><user>abc</user><pwd>123</pwd></command>"); //char a[] = "<command><user>abc</user><pwd>123</pwd></command>"; //doc.parse(a); XmlNode command = doc.firstNode("command"); XmlNode user = command.firstNode("user"); XmlNode pwd = command.firstNode("pwd"); KY_LOG_INFO("user: %s pwd: %s", user.text(), pwd.text()); doc.save("test.xml"); return 0; }
bool Test::testCase1() const { bool pass = true; bool b = false; if (m_verbose) cout << "\tCASE 1: Parsing good document and checking tree\n"; try { XmlDocument doc; doc.parse("./test1.xml"); /* <?xml version="1.0" encoding="utf-8"?> <element1 a="1" b="2"> <element2 c="3" d="true"/> <element3>Hello World!</element3> <element4 a="123"> <element5 yes="no"> <element6 str="This is a string" num="4.5"/> <element7>99.8</element7> </element5> </element4> </element1> */ XmlNode node = doc.firstNode(); node = node.nextSibling(); SUBCASE_CHECK_CRITICAL(!node.isNull()); SUBCASE_CHECK(node.name().compare("element1") == 0, pass, b); XmlAttribute attr = node.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("a") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 1, pass, b); attr = attr.nextAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("b") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 2, pass, b); { XmlNode node_ = node.firstChild(); SUBCASE_CHECK_CRITICAL(!node_.isNull()); SUBCASE_CHECK(node_.name().compare("element2") == 0, pass, b); XmlAttribute attr = node_.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("c") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 3, pass, b); attr = attr.nextAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("d") == 0, pass, b); SUBCASE_CHECK(attr.getString().compare("true") == 0, pass, b); node_ = node_.nextSibling(); SUBCASE_CHECK_CRITICAL(!node_.isNull()); SUBCASE_CHECK(node_.name().compare("element3") == 0, pass, b); SUBCASE_CHECK(node_.getString().compare("Hello World!") == 0, pass, b); node_ = node_.nextSibling(); SUBCASE_CHECK_CRITICAL(!node_.isNull()); SUBCASE_CHECK(node_.name().compare("element4") == 0, pass, b); attr = node_.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("a") == 0, pass, b); SUBCASE_CHECK(attr.getInt() == 123, pass, b); { XmlNode node__ = node_.firstChild(); SUBCASE_CHECK_CRITICAL(!node__.isNull()); SUBCASE_CHECK(node__.name().compare("element5") == 0, pass, b); XmlAttribute attr = node__.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("yes") == 0, pass, b); SUBCASE_CHECK(attr.getString().compare("no") == 0, pass, b); { XmlNode node___ = node__.firstChild(); SUBCASE_CHECK_CRITICAL(!node___.isNull()); SUBCASE_CHECK(node___.name().compare("element6") == 0, pass, b); XmlAttribute attr = node___.firstAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("str") == 0, pass, b); SUBCASE_CHECK(attr.getString().compare("This is a string") == 0, pass, b); attr = attr.nextAttribute(); SUBCASE_CHECK_CRITICAL(!attr.isNull()); SUBCASE_CHECK(attr.name().compare("num") == 0, pass, b); SUBCASE_CHECK(attr.getFloat() == 4.5, pass, b); } } } } catch (XmlException& e) { pass = false; if (m_verbose) { cout << e.what() << "\n"; } } if (m_verbose) cout << (pass ? "\tPASS\n" : "\tFAIL\n"); return pass; }