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; }
ResourceItem* SpriteFontDescriptionImporter::import(const String& filename, io::FileSystem* fileSystem) const { using namespace sani::io; using namespace sani::parser; FileStream* stream = nullptr; // the file exists so we can just open it fileSystem->openFile(filename, Filemode::Read, &stream); // the file points to XML XmlDocument doc; try { doc.load(stream); } catch (const XmlException& ex) { (void)ex; fileSystem->closeFile(filename); throw; } fileSystem->closeFile(filename); XmlNode root, nameNode, sizeNode, spacingNode, regionsNode, outlineNode, outlineTypeNode, outlineWidthNode; std::vector<XmlNode> regionNodes; CharacterRegionCollection characterRegions; doc.firstNode(root); root.firstNode("name", nameNode); root.firstNode("size", sizeNode); root.firstNode("spacing", spacingNode); root.firstNode("character_regions", regionsNode); String nameOfFont(nameNode.value()); String nameOfFontWithoutExtension(nameOfFont); size_t index = 0; if ((index = nameOfFontWithoutExtension.rfind(".")) != String::npos) { nameOfFontWithoutExtension = nameOfFont.substr(0, index); } FontDescription* desc = new FontDescription( nameOfFontWithoutExtension, XmlUtil::get<float>(sizeNode), XmlUtil::get<float>(spacingNode) ); if (root.firstNode("outline", outlineNode)) { if (outlineNode.firstNode("type", outlineTypeNode)) { desc->setOutlineType( static_cast<OutlineType>(XmlUtil::get<int32>(outlineTypeNode) )); } if (outlineNode.firstNode("type", outlineWidthNode)) { desc->setOutlineWidth( (XmlUtil::get<float32>(outlineWidthNode) )); } } regionsNode.getChildNodes(regionNodes); for (auto& regionNode : regionNodes) { XmlNode startNode, endNode; regionNode.firstNode("start", startNode); regionNode.firstNode("end", endNode); unsigned short start = XmlUtil::get<uint32>(startNode); unsigned short end = XmlUtil::get<uint32>(endNode); characterRegions.push_back(std::make_tuple(start, end)); } desc->setSetCharacterRegions(characterRegions); String basename(filename.substr(0, filename.rfind("\\"))); String assetFolderPath(basename + "\\" + nameOfFont); if (fileSystem->fileExists(assetFolderPath)) { desc->setFontPath(assetFolderPath); } else { // it is a system font if (!platformIsFontInstalled(nameOfFont.c_str())){ throw std::runtime_error(String("Can find font ") + nameOfFont); } desc->setFontPath(platformGetFontPath(nameOfFont)); } return desc; }