{"id":399,"date":"2023-03-23T07:18:19","date_gmt":"2023-03-23T07:18:19","guid":{"rendered":"https:\/\/yunshangtulv.com.cn\/?p=399"},"modified":"2023-03-23T07:18:19","modified_gmt":"2023-03-23T07:18:19","slug":"xml%e6%96%87%e4%bb%b6%e8%af%a6%e8%a7%a3%e5%8f%8a%e5%ba%94%e7%94%a8%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/yunshangtulv.com.cn\/?p=399","title":{"rendered":"XML\u6587\u4ef6\u8be6\u89e3\u53ca\u5e94\u7528\u5b9e\u4f8b"},"content":{"rendered":"<div class=\"group w-full text-gray-800 dark:text-gray-100 border-b border-black\/10 dark:border-gray-900\/50 dark:bg-gray-800\">\n<div class=\"text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-2xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto\">\n<div class=\"relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]\">\n<div class=\"flex flex-grow flex-col gap-3\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"group w-full text-gray-800 dark:text-gray-100 border-b border-black\/10 dark:border-gray-900\/50 bg-gray-50 dark:bg-[#444654]\">\n<div class=\"text-base gap-4 md:gap-6 md:max-w-2xl lg:max-w-2xl xl:max-w-3xl p-4 md:py-6 flex lg:px-0 m-auto\">\n<div class=\"w-[30px] flex flex-col relative items-end\">\n<div class=\"relative h-[30px] w-[30px] p-1 rounded-sm text-white flex items-center justify-center\"><\/div>\n<\/div>\n<div class=\"relative flex w-[calc(100%-50px)] flex-col gap-1 md:gap-3 lg:w-[calc(100%-115px)]\">\n<div class=\"flex flex-grow flex-col gap-3\">\n<div class=\"min-h-[20px] flex flex-col items-start gap-4 whitespace-pre-wrap\">\n<div class=\"markdown prose w-full break-words dark:prose-invert dark\">\n<p>R\u8bed\u8a00\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>XML<\/code>\u5305\u6765\u5904\u7406XML\u6587\u4ef6\u3002<code>XML<\/code>\u5305\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u7528\u4e8e\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u64cd\u4f5c\u548c\u5199\u5165XML\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86<code>XML<\/code>\u5305\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\">install.packages<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"XML\"<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e9b\u5e94\u7528\u5b9e\u4f8b\u6765\u8be6\u7ec6\u89e3\u91caR\u8bed\u8a00\u7684XML\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\u8bfb\u53d6\u548c\u89e3\u6790XML\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0bXML\u6587\u4ef6\uff08data.xml\uff09\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-xml\"><span class=\"hljs-meta\">&lt;?xml version=<span class=\"hljs-string\">\"1.0\"<\/span>?&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">employees<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">employee<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">name<\/span>&gt;<\/span>John Doe<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">name<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">age<\/span>&gt;<\/span>30<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">age<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">position<\/span>&gt;<\/span>Software Engineer<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">position<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">employee<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">employee<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">name<\/span>&gt;<\/span>Jane Smith<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">name<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">age<\/span>&gt;<\/span>28<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">age<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">position<\/span>&gt;<\/span>Data Analyst<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">position<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">employee<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">employee<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">name<\/span>&gt;<\/span>Mike Brown<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">name<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">age<\/span>&gt;<\/span>35<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">age<\/span>&gt;<\/span><br \/>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">position<\/span>&gt;<\/span>Project Manager<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">position<\/span>&gt;<\/span><br \/>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">employee<\/span>&gt;<\/span><br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">employees<\/span>&gt;<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u9996\u5148\uff0c\u4f7f\u7528<code>xmlParse<\/code>\u51fd\u6570\u8bfb\u53d6\u5e76\u89e3\u6790XML\u6587\u4ef6\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\">library<span class=\"hljs-punctuation\">(<\/span>XML<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u8bfb\u53d6\u5e76\u89e3\u6790XML\u6587\u4ef6<\/span><br \/>\nxml_data <span class=\"hljs-operator\">&lt;-<\/span> xmlParse<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"data.xml\"<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"2\">\n<li>\u63d0\u53d6XML\u6587\u4ef6\u4e2d\u7684\u6570\u636e<\/li>\n<\/ol>\n<p>\u5728\u89e3\u6790XML\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>xpathSApply<\/code>\u51fd\u6570\u6765\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u63d0\u53d6\u6240\u6709\u5458\u5de5\u7684\u59d3\u540d\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u63d0\u53d6\u6240\u6709\u5458\u5de5\u7684\u59d3\u540d<\/span><br \/>\n<span class=\"hljs-built_in\">names<\/span> <span class=\"hljs-operator\">&lt;-<\/span> xpathSApply<span class=\"hljs-punctuation\">(<\/span>xml_data<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"\/\/name\"<\/span><span class=\"hljs-punctuation\">,<\/span> xmlValue<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p>print<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-built_in\">names<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"3\">\n<li>\u64cd\u4f5cXML\u6587\u4ef6\u4e2d\u7684\u6570\u636e<\/li>\n<\/ol>\n<p>\u4f7f\u7528<code>XML<\/code>\u5305\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9XML\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u5982\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u8282\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5411XML\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5458\u5de5\u8282\u70b9\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u521b\u5efa\u65b0\u7684\u5458\u5de5\u8282\u70b9<\/span><br \/>\nnew_employee <span class=\"hljs-operator\">&lt;-<\/span> newXMLNode<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"employee\"<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\nnewXMLNode<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"name\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"Alice Johnson\"<\/span><span class=\"hljs-punctuation\">,<\/span> parent <span class=\"hljs-operator\">=<\/span> new_employee<span class=\"hljs-punctuation\">)<\/span><br \/>\nnewXMLNode<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"age\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"32\"<\/span><span class=\"hljs-punctuation\">,<\/span> parent <span class=\"hljs-operator\">=<\/span> new_employee<span class=\"hljs-punctuation\">)<\/span><br \/>\nnewXMLNode<span class=\"hljs-punctuation\">(<\/span><span class=\"hljs-string\">\"position\"<\/span><span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"HR Specialist\"<\/span><span class=\"hljs-punctuation\">,<\/span> parent <span class=\"hljs-operator\">=<\/span> new_employee<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u5c06\u65b0\u8282\u70b9\u6dfb\u52a0\u5230XML\u6587\u4ef6<\/span><br \/>\nxmlRoot<span class=\"hljs-punctuation\">(<\/span>xml_data<span class=\"hljs-punctuation\">)<\/span> <span class=\"hljs-operator\">&lt;-<\/span> addChildren<span class=\"hljs-punctuation\">(<\/span>xmlRoot<span class=\"hljs-punctuation\">(<\/span>xml_data<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">,<\/span> new_employee<span class=\"hljs-punctuation\">)<\/span><\/p>\n<p><span class=\"hljs-comment\"># \u67e5\u770b\u66f4\u65b0\u540e\u7684XML\u6587\u4ef6<\/span><br \/>\ncat<span class=\"hljs-punctuation\">(<\/span>saveXML<span class=\"hljs-punctuation\">(<\/span>xml_data<span class=\"hljs-punctuation\">)<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<ol start=\"4\">\n<li>\u5c06\u4fee\u6539\u540e\u7684XML\u6570\u636e\u5199\u5165\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u5c06\u4fee\u6539\u540e\u7684XML\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4f7f\u7528<code>saveXML<\/code>\u51fd\u6570\u4fdd\u5b58XML\u6570\u636e\uff1a<\/p>\n<div class=\"bg-black rounded-md mb-4\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-R\"><span class=\"hljs-comment\"># \u5c06XML\u6570\u636e\u5199\u5165\u65b0\u6587\u4ef6<\/span><br \/>\nsaveXML<span class=\"hljs-punctuation\">(<\/span>xml_data<span class=\"hljs-punctuation\">,<\/span> <span class=\"hljs-string\">\"updated_data.xml\"<\/span><span class=\"hljs-punctuation\">)<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728R\u8bed\u8a00\u4e2d\u5904\u7406XML\u6587\u4ef6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cXML\u6587\u4ef6\u7684\u7ed3\u6784\u548c\u5185\u5bb9\u53ef\u80fd\u66f4\u4e3a\u590d\u6742\uff0c\u4f46\u57fa\u672c\u7684\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u64cd\u4f5c\u548c\u5199\u5165\u65b9\u6cd5\u662f\u76f8\u4f3c\u7684\u3002\u66f4\u591a\u5173\u4e8e<code>XML<\/code>\u5305\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u6587\u6863\u6216\u8bbf\u95ee\u4ee5\u4e0b\u94fe\u63a5\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/www.rdocumentation.org\/packages\/XML\/versions\/3.99-0.6\" target=\"_new\" rel=\"noopener\">XML Package Documentation<\/a><\/li>\n<li><a href=\"https:\/\/cran.r-project.org\/web\/packages\/XML\/index.html\" target=\"_new\" rel=\"noopener\">CRAN: XML Package<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>R\u8bed\u8a00\u4e2d\u53ef\u4ee5\u4f7f\u7528XML\u5305\u6765\u5904\u7406XML\u6587\u4ef6\u3002XML\u5305\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u7528\u4e8e\u8bfb\u53d6\u3001\u89e3\u6790\u3001\u64cd\u4f5c\u548c\u5199\u5165XML\u6587\u4ef6\u3002\u9996\u5148 [&hellip;]<\/p>\n","protected":false},"author":111,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-399","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=399"}],"version-history":[{"count":0,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions"}],"wp:attachment":[{"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunshangtulv.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}