{"id":59691,"date":"2024-10-13T00:01:28","date_gmt":"2024-10-12T16:01:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/59691\/"},"modified":"2024-10-13T00:01:28","modified_gmt":"2024-10-12T16:01:28","slug":"c%e8%aa%9e%e8%a8%80%e6%93%8d%e4%bd%9cxml%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e8%a9%b3%e8%a7%a3-c-xml%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/59691\/","title":{"rendered":"C\u8a9e\u8a00\u64cd\u4f5cXML\u6578\u64da\u5eab\u7684\u8a73\u89e3 (c xml\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"c%e8%aa%9e%e8%a8%80%e6%93%8d%e4%bd%9cxml%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e8%a9%b3%e8%a7%a3-HvgrOjNxtQ\">C\u8a9e\u8a00\u64cd\u4f5cXML\u6578\u64da\u5eab\u7684\u8a73\u89e3<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u7ba1\u7406\u4e2d\uff0cXML\uff08\u53ef\u64f4\u5c55\u6a19\u8a18\u8a9e\u8a00\uff09\u5df2\u6210\u70ba\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u6578\u64da\u683c\u5f0f\u3002\u5b83\u7684\u9748\u6d3b\u6027\u548c\u53ef\u8b80\u6027\u4f7f\u5176\u6210\u70ba\u8a31\u591a\u61c9\u7528\u7a0b\u5e8f\u7684\u9996\u9078\u6578\u64da\u5b58\u5132\u683c\u5f0f\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u64cd\u4f5cXML\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afxml%ef%bc%9f-HvgrOjNxtQ\">\u4ec0\u9ebc\u662fXML\uff1f<\/h2>\n<p>XML\u662f\u4e00\u7a2e\u6a19\u8a18\u8a9e\u8a00\uff0c\u7528\u65bc\u63cf\u8ff0\u6578\u64da\u7684\u7d50\u69cb\u3002\u5b83\u7684\u8a2d\u8a08\u76ee\u7684\u662f\u4f7f\u6578\u64da\u6613\u65bc\u8b80\u53d6\u548c\u7de8\u8f2f\u3002XML\u6587\u4ef6\u7531\u4e00\u7cfb\u5217\u6a19\u7c64\u7d44\u6210\uff0c\u9019\u4e9b\u6a19\u7c64\u5b9a\u7fa9\u4e86\u6578\u64da\u7684\u5c64\u6b21\u7d50\u69cb\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684XML\u793a\u4f8b\uff1a<\/p>\n<pre>\n<code>\n\n    \n        \u5b78\u7fd2C\u8a9e\u8a00<\/\u6a19\u984c>\n        \u5f35\u4e09<\/\u4f5c\u8005>\n        99.99<\/\u50f9\u683c>\n    <\/\u66f8>\n    \n        \u6578\u64da\u7d50\u69cb<\/\u6a19\u984c>\n        \u674e\u56db<\/\u4f5c\u8005>\n        89.99<\/\u50f9\u683c>\n    <\/\u66f8>\n<\/\u66f8\u7c4d>\n<\/code>\n<\/pre>\n<h2 id=\"c%e8%aa%9e%e8%a8%80%e6%93%8d%e4%bd%9cxml%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%96%b9%e6%b3%95-HvgrOjNxtQ\">C\u8a9e\u8a00\u64cd\u4f5cXML\u7684\u57fa\u672c\u65b9\u6cd5<\/h2>\n<p>\u5728C\u8a9e\u8a00\u4e2d\uff0c\u64cd\u4f5cXML\u6578\u64da\u901a\u5e38\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5eab\u3002\u6700\u5e38\u7528\u7684\u5eab\u5305\u62eclibxml2\u548cTinyXML\u3002\u9019\u4e9b\u5eab\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684API\u4f86\u89e3\u6790\u548c\u64cd\u4f5cXML\u6578\u64da\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8libxml2%e8%a7%a3%e6%9e%90xml-HvgrOjNxtQ\">\u4f7f\u7528libxml2\u89e3\u6790XML<\/h3>\n<p>libxml2\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684XML\u89e3\u6790\u5eab\uff0c\u652f\u6301DOM\u548cSAX\u5169\u7a2e\u89e3\u6790\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528libxml2\u89e3\u6790XML\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u5b89\u88ddlibxml2\u5eab\u3002<\/li>\n<li>\u5305\u542b\u76f8\u61c9\u7684\u982d\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528xmlReadFile\u51fd\u6578\u8b80\u53d6XML\u6587\u4ef6\u3002<\/li>\n<li>\u904d\u6b77XML\u6a39\u7d50\u69cb\uff0c\u63d0\u53d6\u6240\u9700\u6578\u64da\u3002<\/li>\n<\/ol>\n<h4 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-HvgrOjNxtQ\">\u793a\u4f8b\u4ee3\u78bc<\/h4>\n<pre>\n<code>\n#include \n#include \n#include \n\nvoid parseXML(const char *filename) {\n    xmlDoc *document = xmlReadFile(filename, NULL, 0);\n    if (document == NULL) {\n        printf(\"\u7121\u6cd5\u6253\u958bXML\u6587\u4ef6n\");\n        return;\n    }\n\n    xmlNode *root = xmlDocGetRootElement(document);\n    xmlNode *currentNode = root-&gt;children;\n\n    while (currentNode) {\n        if (currentNode-&gt;type == XML_ELEMENT_NODE) {\n            printf(\"\u7bc0\u9ede\u540d\u7a31: %sn\", currentNode-&gt;name);\n            xmlNode *childNode = currentNode-&gt;children;\n            while (childNode) {\n                if (childNode-&gt;type == XML_ELEMENT_NODE) {\n                    printf(\"  \u5b50\u7bc0\u9ede\u540d\u7a31: %s, \u503c: %sn\", childNode-&gt;name, xmlNodeGetContent(childNode));\n                }\n                childNode = childNode-&gt;next;\n            }\n        }\n        currentNode = currentNode-&gt;next;\n    }\n\n    xmlFreeDoc(document);\n    xmlCleanupParser();\n}\n\nint main() {\n    parseXML(\"books.xml\");\n    return 0;\n}\n<\/code>\n<\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8tinyxml%e6%93%8d%e4%bd%9cxml-HvgrOjNxtQ\">\u4f7f\u7528TinyXML\u64cd\u4f5cXML<\/h3>\n<p>TinyXML\u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684XML\u89e3\u6790\u5eab\uff0c\u9069\u5408\u5d4c\u5165\u5f0f\u7cfb\u7d71\u548c\u5c0f\u578b\u61c9\u7528\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u76f8\u5c0d\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u4e0b\u8f09\u4e26\u5305\u542bTinyXML\u5eab\u3002<\/li>\n<li>\u4f7f\u7528TinyXML\u63d0\u4f9b\u7684API\u8b80\u53d6\u548c\u5beb\u5165XML\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<h4 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-HvgrOjNxtQ\">\u793a\u4f8b\u4ee3\u78bc<\/h4>\n<pre>\n<code>\n#include \n#include \"tinyxml.h\"\n\nvoid parseXML(const char *filename) {\n    TiXmlDocument doc(filename);\n    if (!doc.LoadFile()) {\n        std::cerr &lt;&lt; &quot;\u7121\u6cd5\u52a0\u8f09XML\u6587\u4ef6: &quot; &lt;&lt; doc.ErrorDesc() &lt;FirstChildElement(\"\u66f8\"); book != NULL; book = book-&gt;NextSiblingElement(\"\u66f8\")) {\n        const char *title = book-&gt;FirstChildElement(\"\u6a19\u984c\")-&gt;GetText();\n        const char *author = book-&gt;FirstChildElement(\"\u4f5c\u8005\")-&gt;GetText();\n        const char *price = book-&gt;FirstChildElement(\"\u50f9\u683c\")-&gt;GetText();\n        std::cout &lt;&lt; &quot;\u66f8\u540d: &quot; &lt;&lt; title &lt;&lt; &quot;, \u4f5c\u8005: &quot; &lt;&lt; author &lt;&lt; &quot;, \u50f9\u683c: &quot; &lt;&lt; price &lt;&lt; std::endl;\n    }\n}\n\nint main() {\n    parseXML(&quot;books.xml&quot;);\n    return 0;\n}\n<\/code>\n<\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HvgrOjNxtQ\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528C\u8a9e\u8a00\u64cd\u4f5cXML\u6578\u64da\u5eab\u53ef\u4ee5\u901a\u904e\u591a\u7a2e\u5eab\u4f86\u5be6\u73fe\uff0c\u6700\u5e38\u898b\u7684\u5305\u62eclibxml2\u548cTinyXML\u3002\u9019\u4e9b\u5eab\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\u4f86\u89e3\u6790\u548c\u64cd\u4f5cXML\u6578\u64da\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u958b\u767c\u8005\u3002\u7121\u8ad6\u662f\u5927\u578b\u61c9\u7528\u9084\u662f\u5c0f\u578b\u9805\u76ee\uff0c\u9078\u64c7\u5408\u9069\u7684\u5eab\u548c\u65b9\u6cd5\u90fd\u80fd\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0eC\u8a9e\u8a00\u64cd\u4f5cXML\u6578\u64da\u5eab\u7684\u6280\u5de7\u8207\u65b9\u6cd5\uff0c\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u548c\u8655\u7406XML\u6578\u64da\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-59691","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=59691"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59691\/revisions"}],"predecessor-version":[{"id":59692,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59691\/revisions\/59692"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=59691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=59691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=59691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}