介绍:
TinyXML 是一个小型的但效率高、功能完整的 XML 解析工具,属于开源中的一员。
它采用 C++ 开发,采用 XML 文档、节点对象访问 XML 文件。下载:
http://sourceforge.net/projects/tinyxml/
加载 XML 文档:
TiXmlDocument doc(filename);
if (!doc.LoadFile()) {
printf("Cannot load XML source file: %s.rn", filename);
return false;
}
遍历 XML 的所有节点:
// ---------------------------------------------------------------------------
// recusive process this node and all children
int XML_GlobalTreeNode(TiXmlNode *rootnode, int (* process)(TiXmlNode *node))
{
// TODO
string name;
TiXmlNode *subnode;
// Process
if (rootnode == NULL) {
printf("GlobalProcessXML: root node is NULLrn");
return -1;
}
// if function process returns zero, searching will be continued
// otherwize(non-zero) searching will be breaked
if (process(rootnode))
return 0;
// Process every child Node
subnode = rootnode->FirstChild();
while (subnode) {
if (!subnode->NoChildren()) {
XML_GlobalTreeNode(subnode, process);
}
else {
if (process(subnode))
return 0;
}
subnode = subnode->NextSibling();
}
return 0;
}
处理节点:
// ---------------------------------------------------------------------------
// register keyword by in XML document
int XML_RegisterKeywords(TiXmlNode *node)
{
string keywords;
if (node->Type() == TiXmlNode::ELEMENT) {
if (!stricmp(node->Value(), "meta")) {
if (!stricmp(node->ToElement()->Attribute("name"), "keywords")) {
keywords = node->ToElement()->Attribute("content");
printf("Register: rntURL=%srntKeyword=%srn",
g_CurrentURL.c_str(), keywords.c_str());
// one XML file can only contains one attribute
// so if we get it, no needs to continue with this file
return 1;
}
}
}
return 0;
}