{"id":1728,"date":"2020-12-04T20:29:14","date_gmt":"2020-12-04T20:29:14","guid":{"rendered":"https:\/\/ded9.com\/?p=1728"},"modified":"2025-12-13T12:05:28","modified_gmt":"2025-12-13T12:05:28","slug":"learning-xml-file-in-r-programming-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/tr\/learning-xml-file-in-r-programming-language\/","title":{"rendered":"Learning XML Files in R Programming Language"},"content":{"rendered":"<p><span style=\"font-size: 12pt; font-family: georgia, palatino, serif;\">XML is a file Format that uses standard <a href=\"https:\/\/en.wikipedia.org\/wiki\/ASCII\" target=\"_blank\" rel=\"noopener\">ASCII<\/a> text; it shares both file formats and data on the World Wide Web, the Internet, and elsewhere.\u00a0<\/span><\/p>\n<p>XML File, Similar to HTML, this language contains markup tags. However, unlike <a href=\"https:\/\/ded9.com\/what-is-html-hypertext-markup-language\/\">HTML<\/a>, where tags describe the structure of the page, in XML, the meaning of the data they contain is explicitly defined.<\/p>\n<p>You can read an XML File in R using the &#8220;XML&#8221; package. This package can be installed using the following Command.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">install.packages (\u201cXML\u201d)<\/pre>\n<\/div>\n<h2>Input data<\/h2>\n<p>Create an XML File by copying the following data into a text editor such as Notepad. Save the File with an .xml extension and select &#8216;All files&#8217; as the File type.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">&lt;RECORDS&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 1 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Rick &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 623.3 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 1\/1\/2012 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; IT &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 2 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Dan &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 515.2 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 9\/23\/2013 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; Operations &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 3 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Michelle &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 611 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 11\/15\/2014 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; IT &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 4 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Ryan &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 729 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 5\/11\/2014 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; HR &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 5 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Gary &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 843.25 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 3\/27\/2015 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; Finance &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 6 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Nina &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 578 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 5\/21\/2013 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; IT &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 7 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Simon &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 632.8 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 7\/30\/2013 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; Operations &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;EMPLOYEE&gt;\r\n\r\n&lt;ID&gt; 8 &lt;\/ID&gt;\r\n\r\n&lt;NAME&gt; Guru &lt;\/NAME&gt;\r\n\r\n&lt;SALARY&gt; 722.5 &lt;\/SALARY&gt;\r\n\r\n&lt;STARTDATE&gt; 6\/17\/2014 &lt;\/STARTDATE&gt;\r\n\r\n&lt;DEPT&gt; Finance &lt;\/DEPT&gt;\r\n\r\n&lt;\/EMPLOYEE&gt;\r\n\r\n&lt;\/RECORDS&gt;<\/pre>\n<\/div>\n<h2>Read the XML File<\/h2>\n<h2><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-258641 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/65b7f0423c14a60c41c2c041_6525256482c9e9a06c7a9d3c2F65b7d194f24445bec7a3f0f2_1f03d085_Thumbnail-1.webp\" alt=\"Read the XML File\" width=\"1920\" height=\"1080\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/65b7f0423c14a60c41c2c041_6525256482c9e9a06c7a9d3c2F65b7d194f24445bec7a3f0f2_1f03d085_Thumbnail-1.webp 1920w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/65b7f0423c14a60c41c2c041_6525256482c9e9a06c7a9d3c2F65b7d194f24445bec7a3f0f2_1f03d085_Thumbnail-1-300x169.webp 300w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/65b7f0423c14a60c41c2c041_6525256482c9e9a06c7a9d3c2F65b7d194f24445bec7a3f0f2_1f03d085_Thumbnail-1-1024x576.webp 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/65b7f0423c14a60c41c2c041_6525256482c9e9a06c7a9d3c2F65b7d194f24445bec7a3f0f2_1f03d085_Thumbnail-1-768x432.webp 768w, https:\/\/ded9.com\/wp-content\/uploads\/2020\/12\/65b7f0423c14a60c41c2c041_6525256482c9e9a06c7a9d3c2F65b7d194f24445bec7a3f0f2_1f03d085_Thumbnail-1-1536x864.webp 1536w\" sizes=\"(max-width: 1920px) 100vw, 1920px\" \/><\/h2>\n<p>The XML File is read by R using the xmlParse() function. This File is stored as a List in R.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Load the package required to read XML files.\r\n\r\nlibrary (\u201cXML\u201d)\r\n\r\n# Also load the other required package.\r\n\r\nlibrary (\u201cmethods\u201d)\r\n\r\n# Give the input file name to the function.\r\n\r\nresult &lt;- xmlParse (file = \"input.xml\")\r\n\r\n# Print the result.\r\n\r\nprint (result)<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">1\r\n\r\nRick\r\n\r\n623.3\r\n\r\n1\/1\/2012\r\n\r\nIT\r\n\r\n2\r\n\r\nDan\r\n\r\n515.2\r\n\r\n9\/23\/2013\r\n\r\nOperations\r\n\r\n3\r\n\r\nMichelle\r\n\r\n611\r\n\r\n11\/15\/2014\r\n\r\nIT\r\n\r\n4\r\n\r\nRyan\r\n\r\n729\r\n\r\n5\/11\/2014\r\n\r\nHR\r\n\r\n5\r\n\r\nGary\r\n\r\n843. 25\r\n\r\n3\/27\/2015\r\n\r\nFinance\r\n\r\n6\r\n\r\nNina\r\n\r\n578\r\n\r\n5\/21\/2013\r\n\r\nIT\r\n\r\n7\r\n\r\nSimon\r\n\r\n632.8\r\n\r\n7\/30\/2013\r\n\r\nOperations\r\n\r\n8\r\n\r\nGuru\r\n\r\n722.5\r\n\r\n6\/17\/2014\r\n\r\nFinance<\/pre>\n<\/div>\n<h2>Get the number of nodes in the XML File.<\/h2>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Load the packages required to read XML files.\r\n\r\nlibrary (\u201cXML\u201d)\r\n\r\nlibrary (\u201cmethods\u201d)\r\n\r\n# Give the input file name to the function.\r\n\r\nresult &lt;- xmlParse (file = \"input.xml\")\r\n\r\n# Extract the root node form the xml file.\r\n\r\nrootnode &lt;- xmlRoot (result)\r\n\r\n# Find number of nodes in the root.\r\n\r\nrootsize &lt;- xmlSize (rootnode)\r\n\r\n# Print the result.\r\n\r\nprint (rootsize)<\/pre>\n<\/div>\n<p>When we execute the above code, we get the following result:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">output\r\n\r\n[1] 8<\/pre>\n<\/div>\n<h2>Details of the first node<\/h2>\n<p>Let&#8217;s take a look at the first parsed File. This File suggests that there are various elements within a high-level node.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Load the packages required to read XML files.\r\n\r\nlibrary (\u201cXML\u201d)\r\n\r\nlibrary (\u201cmethods\u201d)\r\n\r\n# Give the input file name to the function.\r\n\r\nresult &lt;- xmlParse (file = \"input.xml\")\r\n\r\n# Extract the root node form the xml file.\r\n\r\nrootnode &lt;- xmlRoot (result)\r\n\r\n# Print the result.\r\n\r\nprint (rootnode [1])<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">$ EMPLOYEE\r\n\r\n1\r\n\r\nRick\r\n\r\n623.3\r\n\r\n1\/1\/2012\r\n\r\nIT\r\n\r\nattr (\u201cclass\")\r\n\r\n[1] \"XMLInternalNodeList\" \"XMLNodeList\"<\/pre>\n<\/div>\n<h2><span style=\"font-size: 18pt;\">Get different elements of a node.<\/span><\/h2>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Load the packages required to read XML files.\r\n\r\nlibrary (\u201cXML\u201d)\r\n\r\nlibrary (\u201cmethods\u201d)\r\n\r\n# Give the input file name to the function.\r\n\r\nresult &lt;- xmlParse (file = \"input.xml\")\r\n\r\n# Extract the root node form the xml file.\r\n\r\nrootnode &lt;- xmlRoot (result)\r\n\r\n# Get the first element of the first node.\r\n\r\nprint (rootnode [[1]] [[1]])\r\n\r\n# Get the fifth element of the first node.\r\n\r\nprint (rootnode [[1]] [[5]])\r\n\r\n# Get the second element of the third node.\r\n\r\nprint (rootnode [[3]] [[2]])<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">1\r\n\r\nIT\r\n\r\nMichelle<\/pre>\n<\/div>\n<p>For effective data management of large files, we read the data from the XML File as a DataFrame. Data framework process for data analysis.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\"># Load the packages required to read XML files.\r\n\r\nlibrary (\u201cXML\u201d)\r\n\r\nlibrary (\u201cmethods\u201d)\r\n\r\n# Convert the input xml file to a data frame.\r\n\r\nxmldataframe &lt;- xmlToDataFrame (\"input.xml\")\r\n\r\nprint (xmldataframe)<\/pre>\n<\/div>\n<p>When we run the above code, the following result is obtained:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;r&quot;,&quot;mime&quot;:&quot;text\/x-rsrc&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">ID NAME SALARY STARTDATE DEPT\r\n\r\n1 1 Rick 623.30 2012-01-01 IT\r\n\r\n2 2 Dan 515.20 2013-09-23 Operations\r\n\r\n3 3 Michelle 611.00 2014-11-15 IT\r\n\r\n4 4 Ryan 729.00 2014-05-11 HR\r\n\r\n5 NA Gary 843.25 2015-03-27 Finance\r\n\r\n6 6 Nina 578.00 2013-05-21 IT\r\n\r\n7 7 Simon 632.80 2013-07-30 Operations\r\n\r\n8 8 Guru 722.50 2014-06-17 Finance<\/pre>\n<\/div>\n<p>Since the data is now available as a data frame, we can utilize the data frame function to read and process files.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is an XML file?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>XML is a structured data format used to store and exchange data in a readable, hierarchical form.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How can R work with XML files?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>R can read and parse XML files using dedicated packages that convert XML data into usable R objects.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why is XML used in data processing?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>XML is widely used because it is flexible, platform-independent, and suitable for structured data exchange.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>XML is a file Format that uses standard ASCII text; it shares both file formats and data on the World Wide Web, the Internet, and elsewhere.\u00a0 XML File, Similar to HTML, this language contains markup tags. However, unlike HTML, where tags describe the structure of the page, in XML, the meaning of the data they [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":1729,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[174],"tags":[11900],"class_list":["post-1728","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-r-studio","tag-xml-file"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/comments?post=1728"}],"version-history":[{"count":4,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1728\/revisions"}],"predecessor-version":[{"id":266107,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/posts\/1728\/revisions\/266107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media\/1729"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/media?parent=1728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/categories?post=1728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/tr\/wp-json\/wp\/v2\/tags?post=1728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}